Detect rectangles in OpenCV (4.2.0) using Python (3.7), - python-3.x

I am working on a personal project where I detect rectangles (all the same dimensions) and then place those rectangles inside a list in the same order (top-bottom) and then process the information inside each rectangle using some function. Below is my test image.
I have managed to detect the rectangle of interest, however I keep getting other rectangles that I don't want. As you can see I only want the three rectangles with the information (6,9,3) into a list.
My code
import cv2
width=700
height=700
y1=0
y2=700
x1=500
x2=700
img=cv2.imread('test.jpg') #read image
img=cv2.resize(img,(width,height)) #resize image
roi = img[y1:y2, x1:x2] #region of interest i.e where the rectangles will be
gray = cv2.cvtColor(roi, cv2.COLOR_BGR2GRAY) #convert roi into gray
Blur=cv2.GaussianBlur(gray,(5,5),1) #apply blur to roi
Canny=cv2.Canny(Blur,10,50) #apply canny to roi
#Find my contours
contours =cv2.findContours(Canny,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)[0]
#Loop through my contours to find rectangles and put them in a list, so i can view them individually later.
cntrRect = []
for i in contours:
epsilon = 0.05*cv2.arcLength(i,True)
approx = cv2.approxPolyDP(i,epsilon,True)
if len(approx) == 4:
cv2.drawContours(roi,cntrRect,-1,(0,255,0),2)
cv2.imshow('Roi Rect ONLY',roi)
cntrRect.append(approx)
cv2.waitKey(0)
cv2.destroyAllWindows()

There is a feature in Contour called cv2.contourArea for which your contour dimensions are input like this cv2.contourArea(contours) . You can use the condition,
if cv2.contourArea(contours)>#Rectangle area
By using this your problem will be solved

I'd suggest that you get the bounding rectangles of the contours and then sort the rectangles by area descending. Crop the first rectangle by default, then loop through the remaining rectangles and crop them if they're, let's say, >=90% of the first rectangle's area. This will ensure that you have the larger rectangles and the smaller ones are ignored.

Related

Find size of polygon area in Tkinter Canvas, Python

I am creating program that analises areas of user-drawn shapes.
Here is sample of code that creates polygon from dots. Program gets dots from mouse motion. Firstly it draws lines, than erases them and draws figure.
def finish_custom_selection(self, event):
# self.custom_lines_id - list of id of created by mouse motion lines [id1, id2 ...]
# self.canvas_for_selection - tkinter canvas I work with
# self.custom_dots - list of dots coords pairs [(x1, y1), (x2, y2) ...]
for line in self.custom_lines_id:
self.canvas_for_selection.delete(line)
item = self.canvas_for_selection.create_polygon(*self.custom_dots,
dash=(10, 10), width=2,
fill='',
tags="draggable",
outline="blue")
self.custom_dots.clear()
self.custom_lines_id.clear()
So here is my question. How can I calculate size of this polygon area? I know algorithms only for convex polygon, but these area can be completely random. Maybe there are any built-in method I am missing?
If you could find the way tkinter canvas fills its pppygon with color you may be able to tweek the method to produce the area. You get voter up

Transform Plates into Horizontal Using Hough transform

I am trying to transform images that are not horizontal, because they may be slanted.
It turns out that when testing 2 images, this photo that is horizontal, and this one that is not. It gives me good results with the horizontal photo, however when trying to change the second photo that is tilted, it does not do what was expected.
The fist image it's works fine like below with a theta 1.6406095. For now it looks bad because I'm trying to make the 2 photos look horizontally correct.
The second image say that theta is just 1.9198622
I think the error it is at this line:
lines= cv2.HoughLines(edges, 1, np.pi/90.0, 60, np.array([]))
I have done a little simulation on this link with colab.
Any help is welcome.
So far this is what I got.
import cv2
import numpy as np
img=cv2.imread('test.jpg',1)
imgGray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
imgBlur=cv2.GaussianBlur(imgGray,(5,5),0)
imgCanny=cv2.Canny(imgBlur,90,200)
contours,hierarchy =cv2.findContours(imgCanny,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
rectCon=[]
for cont in contours:
area=cv2.contourArea(cont)
if area >100:
#print(area) #prints all the area of the contours
peri=cv2.arcLength(cont,True)
approx=cv2.approxPolyDP(cont,0.01*peri,True)
#print(len(approx)) #prints the how many corner points does the contours have
if len(approx)==4:
rectCon.append(cont)
#print(len(rectCon))
rectCon=sorted(rectCon,key=cv2.contourArea,reverse=True) # Sort out the contours based on largest area to smallest
bigPeri=cv2.arcLength(rectCon[0],True)
cornerPoints=cv2.approxPolyDP(rectCon[0],0.01*peri,True)
# Reorder bigCornerPoints so I can prepare it for warp transform (bird eyes view)
cornerPoints=cornerPoints.reshape((4,2))
mynewpoints=np.zeros((4,1,2),np.int32)
add=cornerPoints.sum(1)
mynewpoints[0]=cornerPoints[np.argmin(add)]
mynewpoints[3]=cornerPoints[np.argmax(add)]
diff=np.diff(cornerPoints,axis=1)
mynewpoints[1]=cornerPoints[np.argmin(diff)]
mynewpoints[2]=cornerPoints[np.argmax(diff)]
# Draw my corner points
#cv2.drawContours(img,mynewpoints,-1,(0,0,255),10)
##cv2.imshow('Corner Points in Red',img)
##print(mynewpoints)
# Bird Eye view of your region of interest
pt1=np.float32(mynewpoints) #What are your corner points
pt2=np.float32([[0,0],[300,0],[0,200],[300,200]])
matrix=cv2.getPerspectiveTransform(pt1,pt2)
imgWarpPers=cv2.warpPerspective(img,matrix,(300,200))
cv2.imshow('Result',imgWarpPers)
Now you just have to fix the tilt (opencv has skew) and then use some threshold to detect the letters and then recognise each letter.
As for a general purpose, I think images need to be normalised first so that we can easily detect the edges.

Is there a way to apply a threshold to an image 1 row at a time

I am trying to apply a threshold to an image 1 row at a time. I want to be able to select the the row the threshold would begin and end. Ex. if I have a 1000 x 1000 image I would like to apply my threshold beginning at row 200 and end at row 850. Currently I am able to apply a threshold to an entire image.
img = cv2.imread("*.png",0)
ret,thresh1 = cv2.threshold(img,127,255,cv2.THRESH_BINARY)
titles = ['Original Image','BINARY']
images = [img, thresh1]
for i in range(2):
plt.subplot(1,2,i+1),plt.imshow(images[i],'gray')
plt.title(titles[i])
plt.xticks([]),plt.yticks([])
plt.show()
There are couple of ways of doing this, so I'll go from simplest and fastest, to more flexible and slower...
Simplest and fastest, if your masked area is very simple like yours:
import cv2
import numpy as np
# Load Paddington as greyscale
img = cv2.imread('paddington.png',0)
# Define a region of interest, in this case entire rows 100-300
ROI = slice(100,300)
# Threshold the region of interest, and reinsert back into image
ret,img[ROI] = cv2.threshold(img[ROI],127,255,cv2.THRESH_BINARY)
Notice I declared the ROI as a variable in just one place so that both sides of the equals sign remain correct if you change the mask's size - maintenance issue avoidance!
If your masked area was not entire rows, you could create a tuple of slices:
# Declare ROI
ROI = slice(100,300),slice(10,390)
# Threshold with mask
ret,img[ROI] = cv2.threshold(img[ROI],127,255,cv2.THRESH_BINARY)
If your masked area is more complicated, e.g. compound shapes, an outline or circular, you can still threshold only the masked region of interest! First you create a mask the same size filled with black, then draw your shapes in white and then apply the threshold to the masked region of interest:
# Make a mask the same size as the image and fill with black
mask = np.zeros_like(img)
# Draw a filled white circle onto the black mask to define a region of interest
mask = cv2.circle(mask,(200,100),100,255,-1) # -1 to fill inside circle
ROI = np.nonzero(mask)
# Threshold the region of interest, and reinsert back into image
ret, mask_thresholded = cv2.threshold(img[ROI],127,255,cv2.THRESH_BINARY)
img[ROI] = mask_thresholded.reshape(-1)
Here is the original image of the young rogue:
Keywords: Python, OpenCV, Numpy, image, image processing, mask, masked, threshold, filter, ROI, region of interest

How to remove the white border from my heat map using python

I've generated 2000 heat maps using seaboard in python3. The problem is that it makes a white border as well. I only want to save the heat map. I want to remove these white borders because I want to train my model based on these heat maps and I think having these borders might mess-up the result. Will having these borders matter since each heat map would have this border?
The code I wrote to generate these heat maps.
for i in range(len(h1)):
ax = sns.heatmap(h1[i], yticklabels = False,xticklabels = False, cbar = False)
fig = ax.get_figure()
fig.savefig(path.join(outpath,"neutral_{0}.png".format(i)))
Actual heat map
What I want:
If you really have same size heat map pictures, you can try trimming them by one more step.
Use PIL(pillow) module to do this work.
For example:
from PIL import Image
for i in range(len(h1)):
im = Image.open("neutral_{0}.png".format(i))
im = im.crop((left, upper, right, lower)) # You have to adjust parameter here
#im = im.crop((100, 75, 300, 150)) # ↓
# you will get an image which size is (width=200, height=75)
im.save("neutral_crop_{0}.png".format(i))
The coordinates of these parameters (left, upper, right, lower) are measured from the top left corner of your input image.

Find contour of a sock that contains patterns

I am trying to figure out how I can isolate a non-uniform sock on a picture.
For now I am using edge detection principally as you can see in my code :
main :
# We import the image
image = importImage(filename)
# Save the shapes variables
height, width, _ = np.shape(image)
# Get the gray scale image in a foot shape
grayImage, bigContourArea = getFootShapeImage(image, True)
minArea = width * height / 50
# Extract all contours
contours = getAllContours(grayImage)
# Keep only the contours that are not too big nor too small
relevantContours = getRelevantContours(contours, minArea, maxArea)
And getAllContours does the following :
kernel = np.ones((5, 5), np.uint8)
# Apply Canny Edge detection algorithm
# We apply a Gaussian blur first
edges = cv2.GaussianBlur(grayIm, (5, 5), 0)
# Then we apply Edge detection
edges = cv2.Canny(edges, 10, 100)
# And we do a dilatation followed by erosion to fill gaps
edges = cv2.dilate(edges, kernel, iterations=2)
edges = cv2.erode(edges, kernel, iterations=2)
_, contours, _ = cv2.findContours(edges, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
Here are some pictures resulting from my code :
Original picture with foot on the drawed shape
Only the biggers contours
All contours
So as you can see there are some parts of the socks that are not taken in the sock contour, and I tried to include the whole sock with several techniques but never succeeded.
I tried the following :
Segmentation using Otsu thresholding, Itti's saliency (In order to have a mask of the sock in the image and avoid all the remaining)
Regroup the smaller contours with the big one to create an even bigger one (But then I can't avoid taking others that are outside the socks)
Do you have an idea on how i can proceed ?
Thanks in advance ! I hope it is clear enough, if you need clarifications just ask.
In order to solve this I had to perform some color detection algorithm in order to detect the white sheet of paper that is here for this special purpose. I did so with the following :
# Define a mask for color I want to isolate
mask = cv2.inRange(image, lowerWhiteVals, upperWhiteVals)
# I also applied some morphological operations on the mask to make it cleaner
Here is the mask image obtained doing so before and after operations:
Then I detect the paper on the image by taking the left-most contour on the mask, and use it as a left boundary, I also split the paper contour to get a bottom boundary well representative.
And for the top and right I used the first sock contour I had, assuming this one will always at least have theses 2 boundaries because of how the socks are.
Once this was done I just took all the contours in my boundaries and created a new contour from that by drawing them all onto a blank image and finding the new contour again (Thanks to #Alexander Reynolds).
I also had to fine tune a bit my algorithm in order to have the more representative contour of the sock at the end and you can see what my final result is on this following image, even if it's not perfect it's more than enough for this small trial with opencv.
Thanks #Alexander for your help. And hope it will help others someday !

Resources