0

I wrote the following code using OpenCV and 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()

This is the image I'm getting as output:

![enter image description here

However, my question is this: how can I crop just the white area?

11
  • 2
    have you tried opencv masks?
    – Kenan
    Commented May 16, 2020 at 16:39
  • 1
    Have you tried creating a mask that filters the white?
    – DPM
    Commented May 16, 2020 at 16:40
  • 1
    It would be helpful to know if that is the original image, or if that is the image obtained after a transformation
    – DPM
    Commented May 16, 2020 at 16:42
  • 1
    No, How can i do it ? am just a beginner i need a function to know the coordinates of white area to crop it Whatever the picture was
    – Chahd Sai
    Commented May 16, 2020 at 16:44
  • 2
    @DPM It s an image obtained from substraction of 2 images
    – Chahd Sai
    Commented May 16, 2020 at 16:45

2 Answers 2

6

This will do it:
First, read the image, convert to grayscale, and force those outer right and bottom strips to black.

import cv2
import numpy as np

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

Now threshold the image, find the coordinates of the white points, and take the minimum and maximum x and y coordinates for the white points .

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

Now you can crop, write, and show the image:

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

Here is the cropped region: cropped region

0
3

Here is one way to do that in Python/OpenCV.

  • Read the input
  • Blacken the white edges at the right and bottom of the image
  • Threshold the image
  • Apply morphology close to create a mask
  • Get the contour
  • Get the bounding box of the contour
  • Crop the image at the bounding box
  • Save the results

Input:

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


Threshold image:

enter image description here

Morphology closed mask:

enter image description here

Contour drawn on input:

enter image description here

Cropped Image:

enter image description here

1
  • Thank you so much, that's great
    – Chahd Sai
    Commented May 16, 2020 at 18:34

Not the answer you're looking for? Browse other questions tagged or ask your own question.