Farbod pm

Color Detection using HSV image in python,Opencv

Today, we are going to talk about color detection in opencv using python. As you know RGB format is a very regular color format these days. But there are other image color spaces that each one has their own advantages and disadvantages. In this situation , HSV color space would be very useful because of showing the colors. For more information check wikipedia.

Now, we are talking about the code. First of all we have to read the image using imread function. Then changing the color space using cvtColor function and after all some noise reduction using erosion and dilate functions.

import cv2
import numpy as np 
from google.colab.patches import cv2_imshow

img = cv2.imread("/content/JSjbB.png")
img_output = img
frame_hsv = cv2.cvtColor(img_output, cv2.COLOR_BGR2HSV)
frame_hsv_blur = cv2.GaussianBlur(frame_hsv, (7, 7), 0)
lower_values = np.array([130, 100,0])
upper_values = np.array([170,255, 255])
mask = cv2.inRange(frame_hsv_blur, lower_values, upper_values)
kernel = np.ones((5,5),np.uint8)
erosion = cv2.erode(mask,kernel,iterations = 3)
dilation = cv2.dilate(erosion,kernel,iterations = 3)

The result will be some thing like this.

last result

And this would be tha result before erosion and dilate.

Crop image using Opencv in python

Crop image using Opencv in python

In this post we will discuss how to crop an image in python. Opencv convert images to numpy array. That is the trick. You just have to select the locaions you want like below.

img = cv2.imread(file_path)
crop_img = img[0:400, 0:300]

If you read your image from file path and save it to img variable. Then you can save the 400*300 pixels of img into crop_image. 0:400 means from zero pixel to 400 pixel. you can run the below code in colab to practice more.

from google.colab.patches import cv2_imshow
import cv2
file = "/content/YRkw2o9L0XmYHICE467aUSE1Ciy2rdGvkgtc2lUG.png"
img = cv2.imread(file)
crop_img = img[0:400, 0:300] # Crop from {x, y, w, h } => {0, 0, 300, 400}
Originalimage befor applying crop algorithm
Original image
Cropped image after applying crop algorithm

More information

If you are new in opencv with python we recommend you to take a look here.

Resize and Flip images using OpenCV in python

Resizing and fliping codes

Resizing and Flipping images would be very useful when you are working on creating new database for training a CNN, RNN or deep learning algorithms. For example I was working with many images that my Prof captured from wheat plants. We were going to apply CNN algorithm to that images. But the number of the images was too low so I put them in an Folder and ran the below code.

import glob
import cv2
file_path = glob.glob("/content/drive/My Drive/Data/Yellow/*")
for file in file_path:
  img = cv2.imread(file)
  #crop_img = img[0:400, 0:300] # Crop from {x, y, w, h } => {0, 0, 300, 400}
  # perform the actual resizing of the image and show it
  #resized = cv2.resize(image, dim, interpolation = cv2.INTER_AREA)
  img_rotate_90_clockwise = cv2.rotate(img, cv2.ROTATE_90_CLOCKWISE)
  cv2.imwrite(file+'_cv_rotate_90_clockwise.jpg', img_rotate_90_clockwise)
  # True

  img_rotate_90_counterclockwise = cv2.rotate(img, cv2.ROTATE_90_COUNTERCLOCKWISE)
  cv2.imwrite(file+'_cv_rotate_90_counterclockwise.jpg', img_rotate_90_counterclockwise)
  # True

  img_rotate_180 = cv2.rotate(img, cv2.ROTATE_180)
  cv2.imwrite(file+'_cv_rotate_180.jpg', img_rotate_180)

As you can see I tried to find all of the files path using “glob” library. Next, I tried to read the images using “imread” function. Also trying to resize them using “resize” function. Finally , save the files using “imwrite” function.

More information

Don not forget that you need opencv library to do these stuff. if you are new in image processing go to our instagram page. Also you can see our related pages here.

How to install libraries/Packages In Python

Pip helps you to install Python libraries. Newest version of python install Pip it’s self but if you lost it try to get the installation file and install Pip.


Open terminal on Ubuntu or CMD in windows. and write this command:

>> pip install <desired package name>


>>python -m pip install <desired package name>

>>python3 -m pip install <desired package name>

The result will be like this if its successful.

Successful installation

Also you can use pip3 install if you want to install a package to your version 3 python.

Remember that if you’re using anaconda you have to go to the conda shell first. Then run the pip command.

>> conda activate <conda_evnvironment >

Some other ways

Sometimes python libraries can not be installed using pip. you have to search it on internet but some of them are like this.

installing pytorch in anaconda

>>conda install -c pytorch pytorch