Opencv Python Script From Gimp Procedure - Grass/hard Surface Edge Detection
Solution 1:
Okay, I finally had time to look at this. I will address each point of yours and then show the changes in the code. Let me know if you have any questions, or suggestions.
Looks like you were able to do this yourself well enough.
1.a. This can be taken care of by blurring the image before doing any processing to it. The following changes to the code were made to accomplish this;
... start_time = time.time() blur_img = cv2.GaussianBlur(img,(5,5),0) #here # Use kmeans to convert to 2 color image hsv_img = cv2.cvtColor(blur_img, cv2.COLOR_BGR2HSV) ...
I have changed the code to remove points that are on a line that perfectly follows the side of the image. It should be basically impossible for a grass edge to also coincide with this.
... allpnts = cv2.approxPolyDP(cnt,epsilon,True) new_allpnts = [] for i in range(len(allpnts)): a = (i-1) % len(allpnts) b = (i+1) % len(allpnts) if ((allpnts[i,0,0] == 0 or allpnts[i,0,0] == (img.shape[1]-1)) and (allpnts[i,0,1] == 0 or allpnts[i,0,1] == (img.shape[0]-1))): tmp1 = allpnts[a,0] - allpnts[i,0] tmp2 = allpnts[b,0] - allpnts[i,0] if not (0 in tmp1 and 0 in tmp2): new_allpnts.append(allpnts[i]) else: new_allpnts.append(allpnts[i]) ... cv2.drawContours(pntscanvas, new_allpnts, -1, (0, 0, 255), 2) ...
Due to how the contours are found in the image, we can simply flip the thresholding function and find the contour around the other part of the image. Changes are below;
... #Extract contours from the mask ret,thresh = cv2.threshold(mask,250,255,cv2.THRESH_BINARY) #here im2,contours,hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) ...
As for the color differences, you have converted your image into HSV format and before saving you are not switching it back to BGR. This change to HSV does give you better results so I would keep it, but it is a different palette. Changes are below;
... cv2.imshow('mask.bmp',mask) res2 = cv2.cvtColor(res2, cv2.COLOR_HSV2BGR) cv2.imwrite('CvKmeans2Color.bmp',res2) cv2.imshow('CvKmeans2Color.bmp',res2) ...
Disclaimer: These changes are based off of the python code from above. Any changes to the python code that are not in the provide code my render my changes ineffective.
Post a Comment for "Opencv Python Script From Gimp Procedure - Grass/hard Surface Edge Detection"