Python OpenCV 从二值图中检测白色物体并裁剪

4 投票
2 回答
11561 浏览
提问于 2025-04-18 14:20

我的目标是从这个二值图像中检测出一张白纸,然后裁剪出这张白纸,制作一个只包含这张白纸的新二值图像。

输入图像描述

现在,我的Python代码使用OpenCV可以找到这张白纸。第一步,我创建了一个用于寻找白纸的掩模:

掩模图像

如你们所见,小的白色噪点和小块已经被去掉了。接下来,问题是我该如何从这个二值图像中裁剪出白纸的部分,以便制作一个新的二值图像呢?

我现在的代码是:

import cv2
import numpy as np

QR = cv2.imread('IMG_0352.TIF', 0) 
mask = np.zeros(QR.shape,np.uint8) 

contours, hierarchy = cv2.findContours(QR,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)

for cnt in contours:
    if cv2.contourArea(cnt)>1000000:
        cv2.drawContours(mask,[cnt],0,255,-1) 

在cnt变量中,有四个元素,但对我来说它们没有意义。

我用代码来拟合一个框:

x,y,w,h = cv2.boundingRect(cnt)
cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),2)

这个框的信息似乎不太对。

感谢任何建议。

后续:我已经解决了这个问题,其实非常简单。代码如下:

import cv2
import numpy as np


QR_orig = cv2.imread('CamR_IMG_0352.TIF', 0)
QR = cv2.imread('IMG_0352.TIF', 0) # read the QR code binary image as grayscale image to make sure only one layer
mask = np.zeros(QR.shape,np.uint8) # mask image the final image without small pieces

# using findContours func to find the none-zero pieces
contours, hierarchy = cv2.findContours(QR,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)

# draw the white paper and eliminate the small pieces (less than 1000000 px). This px count is the same as the QR code dectection
for cnt in contours:
    if cv2.contourArea(cnt)>1000000:
        cv2.drawContours(mask,[cnt],0,255,-1) # the [] around cnt and 3rd argument 0 mean only the particular contour is drawn

        # Build a ROI to crop the QR
        x,y,w,h = cv2.boundingRect(cnt)
        roi=mask[y:y+h,x:x+w]
        # crop the original QR based on the ROI
        QR_crop = QR_orig[y:y+h,x:x+w]
        # use cropped mask image (roi) to get rid of all small pieces
        QR_final = QR_crop * (roi/255)

cv2.imwrite('QR_final.TIF', QR_final)

2 个回答

0

我其实找到了这个问题的解决办法,真的非常简单!!

import cv2
import numpy as np


QR_orig = cv2.imread('CamR_IMG_0352.TIF', 0)
QR = cv2.imread('IMG_0352.TIF', 0) # read the QR code binary image as grayscale image to make sure only one layer
mask = np.zeros(QR.shape,np.uint8) # mask image the final image without small pieces

# using findContours func to find the none-zero pieces
contours, hierarchy = cv2.findContours(QR,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)

# draw the white paper and eliminate the small pieces (less than 1000000 px). This px count is the same as the QR code dectection
for cnt in contours:
    if cv2.contourArea(cnt)>1000000:
        cv2.drawContours(mask,[cnt],0,255,-1) # the [] around cnt and 3rd argument 0 mean only the particular contour is drawn

        # Build a ROI to crop the QR
        x,y,w,h = cv2.boundingRect(cnt)
        roi=mask[y:y+h,x:x+w]
        # crop the original QR based on the ROI
        QR_crop = QR_orig[y:y+h,x:x+w]
        # use cropped mask image (roi) to get rid of all small pieces
        QR_final = QR_crop * (roi/255)

cv2.imwrite('QR_final.TIF', QR_final)
1

轮廓对象是一个任意的点的向量(列表),这些点围绕着被检测到的物体。

一种简单粗暴的方法是,在你进行阈值处理后,遍历所有的像素,然后简单地复制那些白色的像素。

我认为findContours()会改变图像(这是一个副作用),所以要检查一下QR。

不过,通常你需要获取最大的轮廓。

示例:
# Choose largest contour
best = 0
maxsize = 0
count = 0
for cnt in contours:
    if cv2.contourArea(cnt) > maxsize :
        maxsize = cv2.contourArea(cnt)
        best = count

    count = count + 1

x,y,w,h = cv2.boundingRect(cnt[best])
cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),2)

撰写回答