如何使用opencv检测大的白色像素的区域? [英] how to detect region of large # of white pixels using opencv?

查看:2657
本文介绍了如何使用opencv检测大的白色像素的区域?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检测标志里面的图像为了删除它,我有一个想法是寻找具有大量的像素,然后删除的对象,另一个想法是循环通过所有的白色像素(我已经反转我的图像),并寻找像素形成一个大的区域,然后删除这个区域,有任何算法更好的这一个,也是哪个方法在opencv将帮助我检测大像素数目的对象。

解决方案

我有一个方法来做到这一点。我不知道这种方法是否适用于所有,但它在这里工作。



下面是代码(在Python中):



首先将图像转换为灰度,调整图像大小,应用阈值,并制作与调整大小的灰度图像大小和类型相同的掩码图像。 (掩码图片只是一个黑色图片)

  import cv2 
import numpy as np
$ b b img = cv2.imread('bus.png')
img = cv2.resize(img,(400,500))
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
ret ,gray = cv2.threshold(gray,127,255,0)
gray2 = gray.copy()
mask = np.zeros(gray.shape,np.uint8)

现在在阈值图像中找到轮廓。过滤轮廓在500到5000之间的区域。它很可能是一个大的白色blob,显然不是字母。 (记住,这个区域是特别为这个图像。我不知道你的其他图像,你必须找到它自己)。

 轮廓,hier = cv2.findContours(gray,cv2.RETR_LIST ,cv2.CHAIN_APPROX_SIMPLE)
用于轮廓中的cnt:
如果200 cv2.drawContours(img,[cnt],0,(0,255, 0),2)
cv2.drawContours(mask,[cnt],0,255,-1)

以下是检测到的轮廓图像:





接下来是掩码图像: code>





现在使用 cv2.bitwise_not 函数反转图像。在这里,你可以选择给予面具,我们给我们的面具图像,使该功能只对输入图像中的面具有白色的面具图像中的区域操作。

  cv2.bitwise_not(gray2,gray2,mask)

  cv2.imshow('IMG',gray2)
cv2.waitKey(0)
cv2 .destroyAllWindows()

这里是结果:


$ b b

>






注意:



上述方法是为了在白色方块中保留橙色。这就是为什么有一些工件。



只要找到区域过滤的轮廓的边界矩形,并绘制黑色的矩形即可。

代码:

  import cv2 
import numpy as np

img = cv2.imread('bus.png')
img = cv2.resize(img,(400,500))
gray = cv2.cvtColor(img,cv2。 COLOR_BGR2GRAY)
ret,gray = cv2.threshold(gray,127,255,0)
gray2 = gray.copy()

contoururs,hier = cv2.findContours(gray,cv2 .RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)
用于轮廓中的cnt:
如果200< cv2.contourArea(cnt)< 5000:
(x,y,w,h)= cv2.boundingRect (cnt)
cv2.rectangle(gray2,(x,y),(x + w,y + h),0,-1)

cv2.imshow gray2)
cv2.waitKey(0)
cv2.destroyAllWindows()

结果



>



比以前更好,当然如果你不想要ORANGE)


i want to detect logo inside image in order to remove it , i have an idea that's to look for objects which have the big number of pixels then remove , another idea is to loop through all the white pixels(i have inverted my image) and look for pixels which forms a large region and then remove this region, is there's any algorithm better that this one , also which methods in opencv will help me to detect object of large pixels number.

解决方案

I have a method to do this. I don't know whether this method applicable to all, but it works good here.

Below is code ( in Python ):

First convert image to grayscale, resize image, apply threshold, and make a mask image of same size and type of that of resized grayscale image. (Mask image is just a black image)

import cv2
import numpy as np

img = cv2.imread('bus.png')
img = cv2.resize(img,(400,500))
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
ret,gray = cv2.threshold(gray,127,255,0)
gray2 = gray.copy()
mask = np.zeros(gray.shape,np.uint8)

Now find contours in the threshold image. Filter the contour for area between 500 to 5000. It will be most probably a large white blob, obviously not letters. (Remember, this area is particular for this image. I dont know about your other images. You will have to find it yourself). Now draw this contour on the mask image filled with white color.

contours, hier = cv2.findContours(gray,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)
for cnt in contours:
    if 200<cv2.contourArea(cnt)<5000:
        cv2.drawContours(img,[cnt],0,(0,255,0),2)
        cv2.drawContours(mask,[cnt],0,255,-1)

Below is the detected contour image:

Next is the mask image:

Now you invert image using cv2.bitwise_not function. There you have option for giving mask where we give our mask image so that function operates only on the area in input image where there is white in mask image.

cv2.bitwise_not(gray2,gray2,mask)

And finally show the image :

cv2.imshow('IMG',gray2)
cv2.waitKey(0)
cv2.destroyAllWindows()

And here is the result:


NOTE:

Above method is done to preserve "ORANGE" in white square. That is why some artifacts are there. If you don't want that orange also, it can be more accurate.

Just find the bounding rectangle for area-filtered contours and draw rectangle filled with black color.

Code :

import cv2
import numpy as np

img = cv2.imread('bus.png')
img = cv2.resize(img,(400,500))
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
ret,gray = cv2.threshold(gray,127,255,0)
gray2 = gray.copy()

contours, hier = cv2.findContours(gray,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)
for cnt in contours:
    if 200<cv2.contourArea(cnt)<5000:
        (x,y,w,h) = cv2.boundingRect(cnt)
        cv2.rectangle(gray2,(x,y),(x+w,y+h),0,-1)

cv2.imshow('IMG',gray2)
cv2.waitKey(0)
cv2.destroyAllWindows()

Result :

detected bounding rects:

Then fillout those rectangles with black:

It is better than previous , of course if you don't want "ORANGE")

这篇关于如何使用opencv检测大的白色像素的区域?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆