![]() You can do this with the same idea as above, but using cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel). Closing is the opposite operation-removing chunks of black from your image that are surrounded by white. In the above, opening was shown as the method to remove small bits of white from your binary mask. Masked_img = cv2.bitwise_and(img, img, mask=opened_mask) Opened_mask = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel) Kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (15, 15)) The tutorials on that page show how it works nicely. In OpenCV, this is accomplished with cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel). What you want to do is called opening, which is the combined process of eroding (which more or less just removes everything within a certain radius) and then dilating (which adds back to any remaining objects however much was removed). The answer provided by ngalstyan shows how to do this nicely with morphology. Mask = cv2.inRange(hsv, lower_green, upper_green) Hsv = cv2.cvtColor(blur, cv2.COLOR_BGR2HSV) Check out my answer here going in-depth on the HSV colorspace. This is much more easily accomplished in the HSV colorspace. Masked_img = cv2.bitwise_and(img, img, mask=mask)Ĭurrently, you're trying to contain an image by a range of colors with different brightness-you want green pixels, regardless of whether they are dark or light. Mask = cv2.inRange(blur, lower_green, upper_green) My goal is to get somthing like this picture below from matlab implementation:Ī good idea when you're filtering an image is to lowpass the image or blur it a bit that way neighboring pixels become a little more uniform in color, so it will ease brighter and darker spots on the image and keep holes out of your mask. Thanks for your help.Įdit: made a few mistakes when changing the code, updated to what it currently is now and display the 3 images If anybody could direct me to get the same results as my matlab implementation, that would be greatly appreciated. My thought process is after thresholding to remove pixels less than 100 in size, then smoothen the image with blur and fill up the black holes surrounded by white - that is what i did in matlab. why is this and how do i clean the image up? Ideally i would like to isolate only the image of the cabbage. However, the image does not seem to have changed from "green" to "cleaned" despite using the remove_small_objects function. #green = cv2.GaussianBlur(green, (3, 3), 0)Ĭleaned = morphology.remove_small_objects(green, min_size=64, connectivity=2) Green = cv2.inRange(image, greenLower, greenUpper) # Load the image, convert it to grayscale, and blur it slightly # Construct the argument parser and parse the argumentsĪp.add_argument("-i", "-image", required = True, I am trying to remove noise in an image less and am currently running this code import numpy as np
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |