只裁剪白色像素

2024-09-29 07:31:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用OpenCV和Python编写了以下代码:

import cv2    

cap = cv2.VideoCapture(1)

cv2.namedWindow('Original')
cv2.namedWindow('Captured')
cv2.namedWindow('Deffects')

while True:

    ret, frame = cap.read()
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    cv2.imshow('frame', frame)

    if cv2.waitKey(1) == ord('c'):
        cv2.imshow('Captured', gray)
        cv2.imwrite('tswira.jpg', frame)

    if cv2.waitKey(1) == ord('s'):
        img1 = cv2.imread('carte1.jpg', 0)
        img2 = cv2.imread('tswira.JPG', 0)

        img1 = cv2.resize(img1, (250, 250))
        img2 = cv2.resize(img2, (250, 250))

        sub = img1 - img2

        cv2.imshow('Original', img1)
        cv2.imshow('Captured', img2)
        cv2.imshow('Deffects', sub)

    if cv2.waitKey(1) == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

这是我作为输出得到的图像:

![enter image description here

然而,我的问题是:我如何才能只修剪白色区域


Tags: ifcv2frameimg1capimg2imshoworiginal
2条回答

在Python/OpenCV中有一种方法可以做到这一点

  • 读取输入
  • 将图像右侧和底部的白色边缘变黑
  • 对图像设置阈值
  • 应用形态学关闭以创建遮罩
  • 得到轮廓
  • 获取轮廓的边界框
  • 在边界框处裁剪图像
  • 保存结果

输入:

enter image description here

import cv2
import numpy as np

# load image
img = cv2.imread('diff_image.jpg')

# convert to gray
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# threshold input image
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)[1]
hh, ww = thresh.shape

# blacken right and bottom of image
thresh[hh-2:hh, 0:ww] = 0
thresh[0:hh, ww-1:ww] = 0

# apply morphology close
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (21,21))
mask = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel)

# get contour
cntrs = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cntrs = cntrs[0] if len(cntrs) == 2 else cntrs[1]
c = cntrs[0]

# draw contour on input
contour = img.copy()
cv2.drawContours(contour, [c], -1, (0, 0, 255), 1)

# get bounding box coordinates of contour
x,y,w,h = cv2.boundingRect(c)

# crop input
result = img.copy()
result = img[y:y+h, x:x+w]

# save resulting masked image
cv2.imwrite('diff_image_threshold.jpg', thresh)
cv2.imwrite('diff_image_mask.jpg', mask)
cv2.imwrite('diff_image_contour.jpg', contour)
cv2.imwrite('diff_image_cropped.jpg', result)

# display result, though it won't show transparency
cv2.imshow("thresh", thresh)
cv2.imshow("mask", mask)
cv2.imshow("contour", contour)
cv2.imshow("result", result)
cv2.waitKey(0)
cv2.destroyAllWindows()


阈值图像:

enter image description here

形态闭合掩模:

enter image description here

输入时绘制的轮廓:

enter image description here

裁剪图像:

enter image description here

这样就可以了:
首先,读取图像,将其转换为灰度,并强制将右侧和底部的外部条纹变为黑色

import cv2
import numpy as np

img = cv2.imread('dQF8l.jpg')
img=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
img[320:,:]=0
img[:,230:]=0

现在对图像进行阈值设置,找到白点的坐标,并获取白点的最小和最大x和y坐标

ret,thresh = cv2.threshold(img,127,255,cv2.THRESH_BINARY)
white_pt_coords=np.argwhere(thresh)
min_y = min(white_pt_coords[:,0])
min_x = min(white_pt_coords[:,1])
max_y = max(white_pt_coords[:,0])
max_x = max(white_pt_coords[:,1])

现在,您可以裁剪、写入和显示图像:

crop = img[min_y:max_y,min_x:max_x]
cv2.imshow('orig',img)
cv2.imwrite('crop.jpg',crop)
cv2.waitKey(0)

以下是裁剪区域:cropped region

相关问题 更多 >