I got some image of a cattle farm. Each image supposes to cover only two pen (small cattle room). However, the camera also covers neighboring pens. I need to get rid of the areas of the neighboring pens.
Input Image -
The Output image -
I have tried the following command and it does the job. However, it shrinks the size of the image and makes the output of the size of the bounding box generated in line 2. The output becomes smaller than the original image. In this case, the original image is 2560x1440 but the output is 2536x1406.
import cv2
import numpy as np
import matplotlib.pyplot as plt
frame = cv2.imread("input.jpg")
# pts - location of the 4 corners of the roi
pts = np.array([[6, 1425],[953, 20 ],[1934, 40 ], [2541,1340]])
rect = cv2.boundingRect(pts)
x, y, w, h = rect
croped = frame[y:y + h, x:x + w].copy()
pts = pts - pts.min(axis=0)
mask = np.zeros(croped.shape[:2], np.uint8)
cv2.drawContours(mask, [pts], -1, (255, 255, 255), -1, cv2.LINE_AA)
frame_roi = cv2.bitwise_and(croped, croped, mask=mask)
cv2.imwrite("output.jpg", frame_roi)
However, I need the output image to be the same size as the input image and anything out of the ROI to be black/white (shown below, it's a different picture though). Both the white or black masked region will work (the above output has black and hand edited image below has white). Is there a way of doing that with opencv or any other library?
The error was in this line
mask = np.zeros(croped.shape[:2], np.uint8)
which should be the exact same size as your original/input image. So changing that to the original shape should give the correct output image.
mask = np.zeros(original_image.shape, np.uint8)
Here's the shape of the output image
(1440L, 2560L, 3L)
import cv2
import numpy as np
original_frame = cv2.imread("1.jpg")
frame = original_frame.copy()
# pts - location of the 4 corners of the roi
pts = np.array([[6, 1425],[953, 20],[1934, 40], [2541,1340]])
(x,y,w,h) = cv2.boundingRect(pts)
pts = pts - pts.min(axis=0)
mask = np.zeros(original_frame.shape, np.uint8)
cv2.drawContours(mask, [pts], -1, (255, 255, 255), -1, cv2.LINE_AA)
result = cv2.bitwise_and(original_frame, mask)
cv2.imshow('mask', mask)
cv2.imshow('result', result)
cv2.imwrite('result.png', result)
print(result.shape)
cv2.waitKey(0)
Related
I want to use OCR (pytesseract) to recognize the text located in images like these:
I have thousands of these arrows. Until now the procedure is as follows: I first resize the image (for another process). Then I crop the image to get rid of the most part of the arrow. Next I draw a white rectangle as a frame to remove further noise but still have distance between text and image borders for better text recognition. I resize the image again to ensure a height of capital letters to ~30 px (https://groups.google.com/forum/#!msg/tesseract-ocr/Wdh_JJwnw94/24JHDYQbBQAJ). Finally I binarize the image with a threshold of 150.
Full code:
import cv2
image_file = '001.jpg'
# load the input image and grab the image dimensions
image = cv2.imread(image_file, cv2.IMREAD_GRAYSCALE)
(h_1, w_1) = image.shape[:2]
# resize the image and grab the new image dimensions
image = cv2.resize(image, (int(w_1*320/h_1), 320))
(h_1, w_1) = image.shape
# crop image
image_2 = image[70:h_1-70, 20:w_1-20]
# get image_2 height, width
(h_2, w_2) = image_2.shape
# draw white rectangle as a frame around the number -> remove noise
cv2.rectangle(image_2, (0, 0), (w_2, h_2), (255, 255, 255), 40)
# resize image, that capital letters are ~ 30 px in height
image_2 = cv2.resize(image_2, (int(w_2*50/h_2), 50))
# image binarization
ret, image_2 = cv2.threshold(image_2, 150, 255, cv2.THRESH_BINARY)
# save image to file
cv2.imwrite('processed_' + image_file, image_2)
# tesseract part can be commented out
import pytesseract
config_7 = ("-c tessedit_char_whitelist=0123456789AB --oem 1 --psm 7")
text = pytesseract.image_to_string(image_2, config=config_7)
print("OCR TEXT: " + "{}\n".format(text))
The problem is that the text located in the arrow is never centered. Sometimes I remove part of the text with the method described above (e.g. in image 50A).
Is there a method in image processing to get rid of the arrow in a more elegant way? For instance using contour detection and deletion? I am more interested in the OpenCV part than the tesseract part to recognize the text.
Any help is appreciated.
If you look at the pictures you will see that there is a white arrow in the image which is also the biggest contour (especially if you draw a black border on the image). If you make a blank mask and draw the arrow (biggest contour on the image) then erode it a little bit you can perform a per element bitwise conjunction of the actual image and eroded mask. If it is not clear look at the bottom code and comments and you will see that it is actually pretty simple.
# imports
import cv2
import numpy as np
img = cv2.imread("number.png") # read image
# you can resize the image here if you like - it should still work for both sizes
h, w = img.shape[:2] # get the actual images height and width
img = cv2.resize(img, (int(w*320/h), 320))
h, w = img.shape[:2]
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # transform to grayscale
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)[1] # perform OTSU threhold
cv2.rectangle(thresh, (0, 0), (w, h), (0, 0, 0), 2)
contours = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)[0] # search for contours
max_cnt = max(contours, key=cv2.contourArea) # select biggest one
mask = np.zeros((h, w), dtype=np.uint8) # create a black mask
cv2.drawContours(mask, [max_cnt], -1, (255, 255, 255), -1) # draw biggest contour on the mask
kernel = np.ones((15, 15), dtype=np.uint8) # make a kernel with appropriate values - in both cases (resized and original) 15 is ok
erosion = cv2.erode(mask, kernel, iterations=1) # erode the mask with given kernel
reverse = cv2.bitwise_not(img.copy()) # reversed image of the actual image 0 becomes 255 and 255 becomes 0
img = cv2.bitwise_and(reverse, reverse, mask=erosion) # per-element bit-wise conjunction of the actual image and eroded mask (erosion)
img = cv2.bitwise_not(img) # revers the image again
# save image to file and display
cv2.imwrite("res.png", img)
cv2.imshow("img", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Result:
You can try simple Python script:
import cv2
import numpy as np
img = cv2.imread('mmubS.png', cv2.IMREAD_GRAYSCALE)
thresh = cv2.threshold(img, 200, 255, cv2.THRESH_BINARY_INV )[1]
im_flood_fill = thresh.copy()
h, w = thresh.shape[:2]
im_flood_fill=cv2.rectangle(im_flood_fill, (0,0), (w-1,h-1), 255, 2)
mask = np.zeros((h + 2, w + 2), np.uint8)
cv2.floodFill(im_flood_fill, mask, (0, 0), 0)
im_flood_fill = cv2.bitwise_not(im_flood_fill)
cv2.imshow('clear text', im_flood_fill)
cv2.imwrite('text.png', im_flood_fill)
Result:
I want to make an inverse filled rectangle in this picture.
The code I have:
import cv2
lena = cv2.imread('lena.png')
output = lena.copy()
cv2.rectangle(lena, (100, 100), (200, 200), (0, 0, 255), -1)
cv2.addWeighted(lena, 0.5, output, 1 - .5, 0, output)
cv2.imshow('', output)
What I want:
Here's what I would do:
# initialize output
output = np.zeros_like(lena, dtype=np.uint8)
output[:,:,-1] = 255
# this is your box top_x
tx,ly,bx,ry = 100,100,200,200
# copy lena to output
output[tx:bx,ly:ry] = lena[tx:bx,ly:ry]
cv2.addWeighted(lena, 0.5, output, 1 - .5, 0, output);
OUtput:
Here is another way to do it in Python/OpenCV. Though it is not as elegant as the solution from Quang Hoang.
Read the input
Create a red image of the same size
Blend the red image with the input
Create a white image with a black rectangle for the "hole"
Combine the blended image and the original image using the mask
Save the result
Input:
import cv2
import numpy as np
# read image
img = cv2.imread('lena.jpg')
# create red image
red = np.full_like(img,(0,0,255))
# add red to img and save as new image
blend = 0.5
img_red = cv2.addWeighted(img, blend, red, 1-blend, 0)
# create white image for mask base
mask = np.full_like(img, (1,1,1), dtype=np.float32)
# define rectangle for "hole" and draw as black filled on the white base mask
x1,y1,x2,y2 = 100,100,200,200
mask = cv2.rectangle(mask, (x1, y1), (x2, y2), (0, 0, 0), -1)
# combine img and img_red using mask
result = cv2.add(img*(1-mask),img_red*mask).astype(np.uint8)
cv2.imshow('img', img)
cv2.imshow('red', red)
cv2.imshow('img_red', img_red)
cv2.imshow('mask', mask)
cv2.imshow('result', result)
cv2.waitKey(0)
cv2.destroyAllWindows()
# save results
cv2.imwrite('lena_hole_mask.jpg', (255*mask).astype(np.uint8))
cv2.imwrite('lena_plus_red.jpg', result)
Mask:
Result:
I'm using OpenCV 4 - python 3 - to find an specific area in a black & white image.
This area is not a 100% filled shape. It may hame some gaps between the white lines.
This is the base image from where I start processing:
This is the rectangle I expect - made with photoshop -:
Results I got with hough transform lines - not accurate -
So basically, I start from the first image and I expect to find what you see in the second one.
Any idea of how to get the rectangle of the second image?
I'd like to present an approach which might be computationally less expensive than the solution in fmw42's answer only using NumPy's nonzero function. Basically, all non-zero indices for both axes are found, and then the minima and maxima are obtained. Since we have binary images here, this approach works pretty well.
Let's have a look at the following code:
import cv2
import numpy as np
# Read image as grayscale; threshold to get rid of artifacts
_, img = cv2.threshold(cv2.imread('images/LXSsV.png', cv2.IMREAD_GRAYSCALE), 0, 255, cv2.THRESH_BINARY)
# Get indices of all non-zero elements
nz = np.nonzero(img)
# Find minimum and maximum x and y indices
y_min = np.min(nz[0])
y_max = np.max(nz[0])
x_min = np.min(nz[1])
x_max = np.max(nz[1])
# Create some output
output = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
cv2.rectangle(output, (x_min, y_min), (x_max, y_max), (0, 0, 255), 2)
# Show results
cv2.imshow('img', img)
cv2.imshow('output', output)
cv2.waitKey(0)
cv2.destroyAllWindows()
I borrowed the cropped image from fmw42's answer as input, and my output should be the same (or most similar):
Hope that (also) helps!
In Python/OpenCV, you can use morphology to connect all the white parts of your image and then get the outer contour. Note I have modified your image to remove the parts at the top and bottom from your screen snap.
import cv2
import numpy as np
# read image as grayscale
img = cv2.imread('blackbox.png')
# convert to grayscale
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
# threshold
_,thresh = cv2.threshold(gray,0,255,cv2.THRESH_BINARY)
# apply close to connect the white areas
kernel = np.ones((75,75), np.uint8)
thresh = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel)
# get contours (presumably just one around the outside)
result = img.copy()
contours = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
contours = contours[0] if len(contours) == 2 else contours[1]
for cntr in contours:
x,y,w,h = cv2.boundingRect(cntr)
cv2.rectangle(result, (x, y), (x+w, y+h), (0, 0, 255), 2)
# show thresh and result
cv2.imshow("thresh", thresh)
cv2.imshow("Bounding Box", result)
cv2.waitKey(0)
cv2.destroyAllWindows()
# save resulting images
cv2.imwrite('blackbox_thresh.png',thresh)
cv2.imwrite('blackbox_result.png',result)
Input:
Image after morphology:
Result:
Here's a slight modification to #fmw42's answer. The idea is connect the desired regions into a single contour is very similar however you can find the bounding rectangle directly since there's only one object. Using the same cropped input image, here's the result.
We can optionally extract the ROI too
import cv2
# Grayscale, threshold, and dilate
image = cv2.imread('3.png')
original = image.copy()
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]
# Connect into a single contour and find rect
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5,5))
dilate = cv2.dilate(thresh, kernel, iterations=1)
x,y,w,h = cv2.boundingRect(dilate)
ROI = original[y:y+h,x:x+w]
cv2.rectangle(image, (x, y), (x+w, y+h), (36, 255, 12), 2)
cv2.imshow('image', image)
cv2.imshow('ROI', ROI)
cv2.waitKey()
Can anyone give me advice on how to crop the two rectangular boxes and save it?
I already tried this code, but it does not crop very well
import cv2;
import numpy as np;
# Run the code with the image name, keep pressing space bar
# Change the kernel, iterations, Contour Area, position accordingly
# These values work for your present image
img = cv2.imread("your_image.jpg", 0);
h, w = img.shape[:2]
kernel = np.ones((15,15),np.uint8)
e = cv2.erode(img,kernel,iterations = 2)
d = cv2.dilate(e,kernel,iterations = 1)
ret, th = cv2.threshold(d, 150, 255, cv2.THRESH_BINARY_INV)
mask = np.zeros((h+2, w+2), np.uint8)
cv2.floodFill(th, mask, (200,200), 255); # position = (200,200)
out = cv2.bitwise_not(th)
out= cv2.dilate(out,kernel,iterations = 3)
cnt, h = cv2.findContours(out,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
for i in range(len(cnt)):
area = cv2.contourArea(cnt[i])
if(area>10000 and area<100000):
mask = np.zeros_like(img)
cv2.drawContours(mask, cnt, i, 255, -1)
x,y,w,h = cv2.boundingRect(cnt[i])
crop= img[ y:h+y,x:w+x]
cv2.imshow("snip",crop )
if(cv2.waitKey(0))==27:break
cv2.destroyAllWindows()
This is the result. It only crops the smaller box. What I want is for it to crop the two squares.
if you have the coordinates of the rectangles you can try:
cropped = img [y1:y2, x1:x2]
cv2.imwrite('cropped.png', cropped)
The first line crops the image base on the given coordinates assuming (y1
I have a picture( Basic X-Y plot image, where the plot line is in blue color and x,y axis are in black color), where in that I need to detect the edges based on the color. I came across below code, where its detecting all the lines by using canny edge detection and hough algorithm. But I need to detect only the blue color line in this image. What can i do in order to detect that?
Below is the code that I used.
import numpy as np
import cv2
img = cv2.imread('xyplot.png')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
cv2.imshow('grayimage',gray)
edges = cv2.Canny(gray,50,150,apertureSize = 3)
cv2.imshow('edgesimage',edges)
print img.shape[1]
print img.shape
minLineLength=img.shape[1]-300
lines = cv2.HoughLinesP(image=edges,rho=0.02,theta=np.pi/500,
threshold=10,lines=np.array([]),
minLineLength=minLineLength, maxLineGap=100)
a,b,c = lines.shape
for i in range(a):
cv2.line(img, (lines[i][0][0], lines[i][0][1]), (lines[i][0][2],
lines[i][0] [3]), (0, 0, 255), 3, cv2.LINE_AA)
cv2.imshow('edges', edges)
cv2.imshow('result', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Apply a (blue) color mask on your image before converting to gray scale. You can do so by defining the lower and upper boundary of all 3 channels and performing BITWISE_AND on the original image. You will have to play around with the values of the channel ranges to make sure only the pixels you want get captured.
lower = np.array([200, 20, 20], dtype = "uint8")
upper = np.array([255, 100, 100], dtype = "uint8")
mask = cv2.inRange(img, lower, upper)
img = cv2.bitwise_and(img, img, mask = mask)
Note: The three channels in the list are B, G and R respectively.