Now we will cover some basic image operations that will assist us in our goal of helping computers see the way a human does, and export an appropriate output.
The first function we will cover is cv2.cvtColor(img,cv2.CONSTANT). This function converts a colorspace of one image to another, i.e. changing the current picture you have from its normal colors to gray. The constant in this case is a list of OpenCV constants that denote the type of conversion. For example the constants cv2.COLOR_BGR2GRAY and cv2.COLOR_BGR2RGB will convert your images to gray or RGB format especially. The latter is important for cases where you are using OpenCV with other libraries since OpenCV uses BGR natively as opposed to RGB, which other libraries most widely support.
1st Argument-Image Matrix
2nd Argument-OpenCV Constant
Original image
Grayscale from cv2.cvtColor()
The second function is cv2.GaussianBlur, which blurs an image. The question you might be asking yourself here is "Why would you want to blur an image? Wouldn't that make it harder to see?" And you'd be right... if we were talking about standard human vision. For computers, this might actually be more effective for seeing. Since blurring actually makes the image more uniform in terms of color, it might be easier to detect a certain object you might want to identify in an image based on its color. The way the gaussian blur works is that for every individual pixel, it will set that pixel to the average of the color values surrounding it. How many color values can be factored into this calculation is determined by the kernel size, which is a odd-sized square matrix. The center of the kernel would be the pixel whose value you would be changing, and the other pixels are the ones used in the calculation. This would be the method for a standard blur, but gaussian actually assigns weights to each of the pixels surrounding to achieve a more natural blur. The thrid argument is the standard deviation of color values in the x direction.
1st Argument-Image Matrix
2nd Argument-Kernel
3rd Argument-Standard Deviation In X Direction
Original Image
Gaussian Blur from cv2.GaussianBlur()
The next function you will cover is cv2.canny, which detects the edges of images, by detecting a difference in colors. Canny first starts by calculating edges of an image based on not only the difference in colors but how steep that change in colors is. Higher differences will lead to brighter edges, while lower differences lead to more dull edges. The second argument of canny is the lower threshold and the third argument is the upper threshold. Anything beyond with an intensity difference beyond the upper threshold is called a sure edge, and anything with an intensity difference below the lower threshold is a non-edge. To be classified as an edge that shows up in canny, you must either be a sure edge, or one that connects to a sure edge that is not below the lower threshold.
1st Argument-Image Matrix
2nd Argument-Lower Threshold
3rd Argument-Upper Threshold
Original Image
Edges of image from cv2.canny()
cv2.dilate is a function that makes pixel groups larger through a similar process to blurring, using a kernel as a second argument, and the number of times it will loop through an image as a third argument. cv2.erode will revert this process using the same arguments. Dilate is useful when you need to make a pixel group larger, ie, if you wanted to make the edges in canny larger. Note: erosion is not a perfect process, due to the way it functions as an iterable kernel.
1st Argument-Image Matrix
2nd Argument-Kernel
3rd Argument-Iterations
Note: These apply to both functions.
Original image
Dilation from cv2.dilate()
Erosion from cv2.erode()
cv2.threshold is a function that binarizes an image into two colors, most often being black and white. First, threshold will take in two values. Its second argument is the threshold in which you want the intensities to start binarizing. Anything above that value will binarize into the pixel intensity of the third argument, and anything below that will go to black. Use the cv2.THRESH_BINARY constant to binarize to black and white. NOTE: threshold will output two values, and your second value is the actual matrix output. Assign it to a tuple.
1st Argument-Image Matrix
2nd Argument-Threshold
3rd Argument-Intensity of White Pixels
4th Argument-OpenCV Constant
Original image
Thresholded image from cv2.threshold()
NOTE: you can actually stack/ layer these functions. For example if you take the output of the blurred matrix and put that through canny, it will do a good job of typically finding the outer edges of an image. Use this knowledge to your advantage.
Original image
Blur layer
Canny Layer
Copy and paste the code into your files, and go ahead and run it. The windows will pop up one by one, so don't forget to press the associated keys to close the windows.
##Feel free to change any of the integer values now that you know what they indicate.
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
cv.imshow('Gray',gray)
cv.waitKey(0)
blur = cv.GaussianBlur(img, (5,5),cv.BORDER_DEFAULT)
cv.imshow('blur',blur)
cv.waitKey(0)
canny = cv.Canny(img, 1, 200)
cv.imshow('canny',canny)
cv.waitKey(0)
canny = cv.Canny(blur,1,200)
cv.imshow('canny',canny)
cv.waitKey(0)
dilated = cv.dilate(img,(3,3),iterations=20)
cv.imshow('dilate',dilated)
cv.waitKey(0)
eroded= cv.erode(dilated,(3,3),iterations=20)
cv.imshow('Eroded',eroded)
cv.waitKey(0)
ret, thresh = cv.threshold(gray, 125, 255, cv.THRESH_BINARY)
cv.imshow('Thresh', thresh)
cv.waitKey(0)
#Don't worry about this yet. It will be explained in the next module.
cropped = img[img.shape[0]//2:img.shape[0], img.shape[1]//2:img.shape[1]]
cv.imshow('cropped', cropped)
cv.waitKey(0)
Legend:
img=your specified image matrix
x= an integer value
y= an integer value
cv2.CONSTANT= a value that tells OpenCV what to do specifically with the said function. Think of it as a setting if you will.
Functions:
cv2.cvtColor(img,cv2.CONSTANT)
cv2.GaussianBlur(img,(x,x),y)
cv2.Canny(img,x,y)
cv2.dilate(img,(x,x),iterations=y)
cv2.erode(img,(x,x),iterations=y)
ret, thresh=cv2.threshold(img,x,y,cv2.CONSTANT)