1

If I capture a picture from production line, how can I extract the part I want from image

from pic - https://ibb.co/k9wfTT0
to pic - 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)  

my output: https://ibb.co/rkbQy7w


from zxing and zbar tutorial:
https://learnopencv.com/barcode-and-qr-code-scanner-using-zbar-and-opencv/

can detect QR code location, then if I make the locate point larger, might can include the area/sticker I want https://i.stack.imgur.com/4p6hx.jpg


(plan B)
To public, can I just directly cut to small pieces, instead of do other process, cause I can just OCR img to text only my desire part

the pic link - https://i.stack.imgur.com/3n3EK.jpg

  • there are zxing and zbar library to be able to read qr or barcode codes. – Yunus Temurlenk Nov 03 '22 at 11:57
  • Is this a question from some class? This was just asked a few posts ago. See https://stackoverflow.com/questions/74299517/python-opencv-extract-picture-from-picture and Mark Setchell's answer. – fmw42 Nov 03 '22 at 16:38
  • @fmw42 , yeah I am a student in about 45 people class, cause program is not as other assignment could try as hard as you can then solution can come up, for this if you stock you just stock, I just learn python opencv not more than a month, therefore I need some advice thanks –  Nov 04 '22 at 01:05
  • Threshold on the white of the labels. Then get all the contours for the white regions. Then crop your original image at each of the contour bounding boxes. Then do pytesseract to read the black text. – fmw42 Nov 04 '22 at 01:44
  • hi @fmw42 , I think that ran too far, if we can directly locate qr code location, the (x,y) on image, then just extend the x, y that can include my expect whole sticker [link](https://imgur.com/a/UnGdjdD) –  Nov 04 '22 at 06:46
  • But to locate the QR code, you first need to extract the white regions – fmw42 Nov 04 '22 at 15:24
  • HI @FMW42 , can I just directly cut to small pieces, instead of do other process, cause I can just OCR img to text only my desire part –  Nov 07 '22 at 02:12
  • @DC con. Sorry, I do not understand your question. In order to cut those regions, you would have to find them first. To find them, I suggest thresholding on the white using cv2.inRange(). – fmw42 Nov 07 '22 at 16:08

1 Answers1

1

by this discussion Set white color outside boundingBox (Python, OpenCV)

the code is:

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() 

the link is the output: https://i.stack.imgur.com/xh5MX.jpg