0

I wrote a code which finds utility meter measurement result part and crops it. The problem is it does the resize of image. How can I do it without resizing and at the end I need parameters of found part of images, such as width, heith, center x and y.

import numpy as np
import cv2
import sys
from PIL import Image
import imutils
import os.path 

if len(sys.argv) != 3:
    print ("%s input_file output_file" , sys.argv[0])
    sys.exit()
else:
    input_file = sys.argv[1]
    output_file = sys.argv[2]

if not os.path.isfile(input_file):
    print ("No such file '%s'" , input_file)
    sys.exit()

img = cv2.imread(input_file)
img = imutils.resize(img, height=500)

## Change to LAB space
lab = cv2.cvtColor(img, cv2.COLOR_BGR2LAB)
l,a,b = cv2.split(lab)
imglab = np.hstack((l,a,b))

## normalized the a channel to all dynamic range
na = cv2.normalize(a, None, alpha=0, beta=255, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_8UC1) 

## Threshold to binary
retval, threshed = cv2.threshold(na, thresh = 180,  maxval=255, type=cv2.THRESH_BINARY)    
## Do morphology
kernel = cv2.getStructuringElement( cv2.MORPH_ELLIPSE , (3,3))
opened = cv2.morphologyEx(threshed, cv2.MORPH_OPEN,kernel)
res = np.hstack((threshed, opened)) 
## Find contours
_, contours, hierarchy = cv2.findContours(opened, mode=cv2.RETR_LIST, method=cv2.CHAIN_APPROX_SIMPLE)    
## Draw Contours
res = img.copy()
cv2.drawContours(res, contours, -1, (255,0,0), 10)
## Filter Contours
for idx, contour in enumerate(contours):
    bbox = cv2.boundingRect(contour)
    area = bbox[-1]*bbox[-2]
    if area < 2150:
        continue
    (x, y, w, h) = cv2.boundingRect(contour)
    print(x, y, w, h)
    cv2.rectangle(res, (0, y), (x + w, y + h), (0, 255, 0), 4)
    crop_img = img[y:y+h*2, 0:x+w]
    (w, h, c) = img.shape
    print(w, h)
cv2.imwrite(output_file, crop_img)

Input image is enter image description here

  • I don’t understand your question - why is the resize a problem? You wrote the code with the resize, didn’t you? Why did you include the resize? – DisappointedByUnaccountableMod Nov 22 '18 at 20:23
  • Actually this code was made with my friend long time ago. Now I have to do the same without resizing, when I just comment out resizing part It gives another result. I have to do it without resizing, because I have analize the results (e.g intersection over union) – Perizat Abduraimova Nov 24 '18 at 13:21
  • So show the results this code makes you expect - because it is impossible to help someone who says "it doesn't work" without any specific - and edit *your* code that you are using *now* into the question and show the results it gives, for the *exact* same source image, and make clear why you think your code is giving the wrong result (just being "different" is not necessarily wrong, perhaps the original results weren't so correct). Also you might want to ask your friend why the image is resized. – DisappointedByUnaccountableMod Nov 24 '18 at 15:09
  • I think you mean ANALYZE – DisappointedByUnaccountableMod Nov 25 '18 at 21:52

0 Answers0