Reputation: 15
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:
However, my question is this: how can I crop just the white area?
Upvotes: 0
Views: 1874
Reputation: 53182
Here is one way to do that in Python/OpenCV.
Input:
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:
Morphology closed mask:
Contour drawn on input:
Cropped Image:
Upvotes: 3
Reputation: 5788
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)
Upvotes: 6