(Python,openCV)从大图像中捕捉二维码贴纸图像



如果我从生产线上拍摄图片,我如何从图像中提取我想要的零件

来自图片-https://ibb.co/k9wfTT0
到图片-https://ibb.co/BKfMx6w

import cv2
im = cv2.imread('/home/joy/桌面/test_11_2/split_img/original.png')
gray=cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
contours, hierarchy = cv2.findContours(gray,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)[-2:]
idx =0 
for cnt in contours:
idx += 1
x,y,w,h = cv2.boundingRect(cnt)
roi=im[y:y+h,x:x+w]

cv2.rectangle(im,(x,y),(x+w,y+h),(200,0,0),2)
cv2.imshow('img',im)
cv2.waitKey(0)  

我的输出:https://ibb.co/rkbQy7w


来自zxing和zbar教程:
https://learnopencv.com/barcode-and-qr-code-scanner-using-zbar-and-opencv/

可以检测二维码的位置,然后如果我将定位点调大,可能会包括我想要的区域/贴纸https://i.stack.imgur.com/4p6hx.jpg

  • 下面的讨论可能会解决我的问题(通过扩展定位qr码(x,y(以覆盖我的贴纸部分(Python-使用OpenCV从图像中检测二维码并裁剪

(计划B(
对公众来说,我可以直接切成小块吗,而不是做其他处理,因为我只能OCR img来只显示我想要的部分

图片链接-https://i.stack.imgur.com/3n3EK.jpg

通过此讨论设置边界框外的白色(Python,OpenCV(

代码是:

import cv2
import numpy as np
image = cv2.imread("/home/student_Joy/desktop/test_11_8/original.png")
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
white_bg = 255*np.ones_like(image)
ret, thresh = cv2.threshold(gray, 60, 255, cv2.THRESH_BINARY_INV)
blur = cv2.medianBlur(thresh, 1)
kernel = np.ones((10, 20), np.uint8)
img_dilation = cv2.dilate(blur, kernel, iterations=1)
im2, ctrs, hier = cv2.findContours(img_dilation.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
sorted_ctrs = sorted(ctrs, key=lambda ctr: cv2.boundingRect(ctr)[0])
for i, ctr in enumerate(sorted_ctrs):
# Get bounding box
x, y, w, h = cv2.boundingRect(ctr)
roi = image[y:y + h, x:x + w]
if (h > 50 and w > 50) and h < 200:
cv2.rectangle(image, (x, y), (x + w, y + h), (255, 255, 255), 1)        
cv2.imshow('{}.png'.format(i), roi)
cv2.imwrite(f"/home/student_Joy/desktop/test_11_8/output01/output_{x}_{y}.png", roi)
#--- paste ROIs on image with white background 
white_bg[y:y+h, x:x+w] = roi
cv2.imshow('white_bg_new', white_bg)
cv2.imwrite(f"/home/student_Joy/desktop/test_11_8/output01/final_output_{x}_{y}.png", white_bg)
cv2.waitKey(0)
cv2.destroyAllWindows() 

链接是输出:https://i.stack.imgur.com/xh5MX.jpg

最新更新