【问题标题】:background subtraction or change of background colour of contour detected in Python在 Python 中检测到的背景减去或改变轮廓的背景颜色
【发布时间】:2019-02-09 10:20:16
【问题描述】:

我想删除background 中的黑色部分或将其更改为不同的颜色,因为我试图检测轮廓内的暗像素,这似乎不起作用,因为两者都处于黑色背景和黑暗中像素是黑色的...

这是我尝试过的,但请建议任何其他可能的方法。提前谢谢

我尝试提取黑色背景并将其更改为yellow

请在下面找到我的代码:

import cv2
import numpy as np
mask_color= (0.0,0.0,1.0)

#reading the image
img= cv2.imread('notused.jpg')

#convering the image into grayscale
gray_image= cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

#applying threshold
ret,thresh= cv2.threshold(gray_image,70,255,cv2.THRESH_BINARY)

#finding contours on the original image
_, contours,hierarchy =cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)

#creating a mask as of the image size which is a black image
mask= np.zeros(img.shape[:2],np.uint8)

#inverted to get a white image
maskinv=cv2.bitwise_not(mask)

#drawn contours on the white image
mask_contours=cv2.drawContours(maskinv,contours,0,0,-3)

#converted to 3 channels
mask_stack= np.dstack([mask_contours]*3)


#to get only the background and remove all the contours 
img1=cv2.bitwise_xor(img,img,mask)

#changing every pixel of the background image to yellow
for y in range(img1.shape[0]-1): #row values
    for x in range(img1.shape[1]-1): #column values
        img1[y,x]=(0,255,255)

然后我得到了帮助:How do I remove the background from this kind of image?

这是为了将原始图像与创建的背景混合,但似乎不起作用并给出错误

mask_stack= mask_contours.astype('float32')/255.0
img1=img1.astype('float32')/255.0

masked= (mask_stack *img1)+((1-mask_stack)*mask_color)
masked=(masked*255).astype('uint8')

cv2.waitKey(0)
cv2.destroyAllWindows()

这是错误信息: 请注意,我的 img1 和蒙版形状相同

Traceback(最近一次通话最后一次):

文件“”,第 1 行,在 runfile('C:/Users/User/Anaconda3/darkpixeldetection.py', wdir='C:/Users/User/Anaconda3')

文件 "C:\Users\User\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", 第 678 行,在运行文件中 execfile(文件名,命名空间)

文件 "C:\Users\User\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", 第 106 行,在 execfile 中 exec(编译(f.read(),文件名,'exec'),命名空间)

文件“C:/Users/User/Anaconda3/darkpixeldetection.py”,第 69 行,在 masked= (mask_stack *img1)+((1-mask_stack)*mask_color)

ValueError:操作数无法与形状一起广播 (1540,2670) (1540,2670,3)

img1.shape 的输出:

(1540,2670,3)

mask_stack.shape 的输出:

(1540,2670,3)

弗雷德: 这是我拥有的input image,其内容模糊。 这是删除不必要的轮廓后得到的output image 这是我的代码:

import numpy as np
import cv2
img_original= cv2.imread('blueimagewithblur.jpg')
img_array=np.asarray(img_original)
blur= cv2.pyrMeanShiftFiltering(img_original,21,49)

gray_image= cv2.cvtColor(blur, cv2.COLOR_BGR2GRAY)

ret,thresh= cv2.threshold(gray_image,70,255,cv2.THRESH_BINARY)

_, contours,hierarchy =cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

countourimage=cv2.drawContours(img_original,contours,-1,0,3)
largest_area= 2000

for i,c in enumerate(contours):
    contour_areas=cv2.contourArea(c)
    if(contour_areas>largest_area):
        del contours[i]
        x_rect,y_rect,w_rect,h_rect=cv2.boundingRect(c)
        cropped=img_original[y_rect:y_rect+h_rect,x_rect:x_rect+w_rect]

cv2.imwrite('C:/Users/User/Anaconda3/stackoverflowexam.jpg',cropped)
cv2.imshow('croopedd',cropped)


cv2.waitKey(0)
cv2.destroyAllWindows()

【问题讨论】:

    标签: python opencv image-processing computer-vision opencv-contour


    【解决方案1】:

    您可以利用背景占据图像大部分的事实。如果您知道要检测的总是小于某个大小,则可以使用轮廓区域来过滤要忽略的轮廓。

    maxArea  = 12412 # whatever makes sense in your case
    for i, contour in enumerate(contours):
      area = cv2.contourArea(contour)
      if area > maxArea :
        del contours[i]
    

    【讨论】:

    • 感谢您的回复,当我试图找到图像中最大的轮廓时,它实际上发现那个正方形是最大的,我执行以下步骤然后将其保存到图像中,这就是实际的问题是,当我得到这个裁剪的图像时,因为它并不完全平行于地面并且是倾斜的,所以我在裁剪时也会得到黑色背景..在下一条评论中找到步骤
    • largest_area=0 for i, c in enumerate(contours): contour_area=cv2.contourArea(c) if (contour_area > maximum_area): maximum_area=contour_area x, y, w, h = cv2.boundingRect (c)cropped= image[y :y + h, x : x + w ] cv2.imshow('cropped_region',cropped) cv2.imwrite('Path',cropped) cv2.waitKey(0)
    • 你不想找到最大的轮廓。您想找到小于某个区域的轮廓。假设您的圆圈大约小于 200 x 200 像素。比你的 maxArea 将是 40000。
    • 其实我有150张图片,每张图片都有倾斜的屏幕,其中一些屏幕上的内容也很模糊,我在前面添加了图片供参考,检查:)。所以我设置我的maxarea = 2000(因为我的缺陷像素可能是那个大小),正如你所说的,我想要屏幕和这些像素在里面,我们可以删除轮廓>而不是这个最大区域来摆脱图像的黑色部分。在图像模糊的情况下,它给我的输出完全不正确,它消除了整个液晶屏幕并给我输出内容模糊
    • largest_area=2000 for i,c in enumerate(contours): contour_areas=cv2.contourArea(c) if(contour_areas>largest_area): del contours[i] x_rect,y_rect,w_rect,h_rect=cv2 .boundingRect(c)cropped=img_original[y_rect:y_rect+h_rect,x_rect:x_rect+w_rect]
    猜你喜欢
    • 2016-10-29
    • 2018-09-30
    • 1970-01-01
    • 2022-01-09
    • 2012-04-02
    • 2013-08-20
    • 1970-01-01
    相关资源
    最近更新 更多