Crop the specific color region and remove the noisy regions (Python+OpenCV)
从彩色图像获取二进制图像时遇到问题。
样品
裂纹:
典型的
理想的一个:
我的第一个目标是制作第二张图片作为第三张图片。我想获得具有最大面积的轮廓并删除其他轮廓(也适用于
第二个问题是我上面描述的想法不适用于第一张图像(裂缝)。这种图像可以被丢弃。但无论如何它应该被标记为裂纹。到目前为止,我没有这方面的想法。
我做了什么
这是输入图像和代码 42_1.jpg
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | class Real: __ex_low=np.array([100,30,60]) __ex_high=np.array([140,80,214]) __ob_low=np.array([25,60,50]) #27,65,100]) __ob_high=np.array([50,255,255]) #[45,255,255]) def __opening(self, mask): kernel = np.ones((3,3), np.uint8) op = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel) return op def __del_ext(self, img_got): img = img_got[0:300,] hsv = cv2.cvtColor(img,cv2.COLOR_BGR2HSV) mask = cv2.inRange(hsv, self.__ex_low, self.__ex_high) array1 = np.transpose(np.nonzero(mask)) array2 = np.nonzero(mask) temp=array1.tolist() xmin=min(array2[0]) #find the highest point covered blue x,y,channel=img.shape img=img[xmin:x,] hsv=hsv[xmin:x,] return img, hsv def __init__(self, img_got): img, hsv = self.__del_ext(img_got) mask_temp = cv2.inRange(hsv, self.__ob_low, self.__ob_high) mask = self.__opening(mask_temp) array1 = np.transpose(np.nonzero(mask)) array2 = np.nonzero(mask) ymin=min(array2[1]) ymax=max(array2[1]) xmin=min(array2[0]) xmax=max(array2[0]) self.x = xmax-xmin self.y = ymax-ymin self.ratio = self.x/self.y # xmargin = int(self.x*0.05) #ymargin = int(self.y*0.05) self.img = img[(xmin):(xmax),(ymin):(ymax)] self.mask = mask[(xmin):(xmax),(ymin):(ymax)] #models = glob.glob("D:/Python36/images/motor/*.PNG") img = cv2.imread("D:/Python36/images/0404/33_1.jpg")#<- input image #last_size = get_last_size(models[-1]) #m2= Model(models[39],last_size) r1 = Real(img) cv2.imshow("2",r1.img) cv2.imshow("3",r1.mask) |
如果代码是用
总的来说,你的方法是可以的,除了错误的内核删除了水平线。
我按照以下步骤完成:
(1) Read and convert to HSV
(2) Find the target yellow color region in HSV
(3) morph-op to remove horizone lines
(4) crop the region
这是结果:
代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | #!/usr/bin/python3 # 2018/04/16 13:20:07 # 2018/04/16 14:13:03 import cv2 import numpy as np ## (1) Read and convert to HSV img = cv2.imread("euR2X.png") hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) ## (2) Find the target yellow color region in HSV hsv_lower = (25, 100, 50) hsv_upper = (33, 255, 255) mask = cv2.inRange(hsv, hsv_lower, hsv_upper) ## (3) morph-op to remove horizone lines kernel = np.ones((5,1), np.uint8) mask2 = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel) ## (4) crop the region ys, xs = np.nonzero(mask2) ymin, ymax = ys.min(), ys.max() xmin, xmax = xs.min(), xs.max() croped = img[ymin:ymax, xmin:xmax] pts = np.int32([[xmin, ymin],[xmin,ymax],[xmax,ymax],[xmax,ymin]]) cv2.drawContours(img, [pts], -1, (0,255,0), 1, cv2.LINE_AA) cv2.imshow("croped", croped) cv2.imshow("img", img) cv2.waitKey() |
在 open cv 中检测橙色的推荐颜色空间是什么?
在图像中查找单色、水平空间