How to obtain combined convex Hull of multiple separate shapes - python

I have 2 shapes (pic 1) and need to find one convexHull of both of them combined (pic2). More precisely I am interested in obtaining external corners (purple circles pic 2). The shapes are detached. The shape I trace is a square sheet of transparent plastic with two color stripes on the side. Stripes are very easy to trace (inRange).
One quick and dirty method I am thinking is to connect centers of the stripes with a white line and then obtain convexHull. I am also thinking on concatenating lists of vertexes of both shapes and obtain combined convexHull but I am not certain if this method will crash the convexHull function.
Is there any more elegant way to resolve this problem?
Please help
Pic 1
Pic 2

Issue resolved.
Works like a charm. Concatenating points of separate shapes don't crash convexHull.
I posted the code on GitHub https://github.com/wojciechkrukar/OpenCV/blob/master/RectangleDetector/RectangleDetector.ipynb
This is the result:
Here is the most important chunk of code:
_ , contours,hier = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
#size of contour points
length = len(contours)
#concatinate poits form all shapes into one array
cont = np.vstack(contours[i] for i in range(length))
hull = cv2.convexHull(cont)
uni_hull = []
uni_hull.append(hull) # <- array as first element of list
cv2.drawContours(image,uni_hull,-1,255,2);

import numpy as np
import cv2
img = cv2.imread('in1.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
(_, thresh) = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)
thresh = ~thresh
points = np.column_stack(np.where(thresh.transpose() > 0))
hull1 = cv2.convexHull(points)
result1 = cv2.polylines(img.copy(), [hull1], True, (0,0,255), 2)
cv2.imshow('result1', result1)
points2 = np.fliplr(np.transpose(np.nonzero(thresh)))
approx = cv2.convexHull(points2)
result2 = cv2.polylines(img.copy(), [approx], True, (255,255,0), 2)
cv2.imshow('result2', result2)
(contours, _) = cv2.findContours(thresh, cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE)
points3 = [pt[0] for ctr in contours for pt in ctr]
points3 = np.array(points3).reshape((-1,1,2)).astype(np.int32)
hull3 = cv2.convexHull(points3)
result3 = cv2.drawContours(img.copy(), [hull3], -1, (0,255,0), 1, cv2.LINE_AA)
cv2.imshow('result3', result3)
points4 = list(set(zip(*np.where(img >= 128)[1::-1])))
points4 = np.array(points4).reshape((-1,1,2)).astype(np.int32)
hull4 = cv2.convexHull(points4)
result4 = cv2.drawContours(img.copy(), [hull4], -1, (0,255,255), 1, cv2.LINE_AA)
cv2.imshow('result4', result4)
result = np.hstack([result1, result2, result3, result4])
cv2.imwrite('result.jpg', result)

Related

How to crop a region of small pixels in an image using OpenCv in Python

I would like to crop out a region full of small curves in an image.
The original image is like the following:
Using the opening morphing, I can remove most of the noises. The result is like:
I tried to use dilate to connect these pixels in my desired region, but the result is not satisfactory.
Is there any function in opencv that can locate this region?
You are on the right track, here's an approach using morphological transformations
Convert image to grayscale and Gaussian blur
Otsu's threshold
Perform morphological operations
Find contours and filter using maximum area
Extract ROI
The idea is to connect the desired region into a single contour then filter using maximum area. This way, we can grab the region as one piece. Here's the detected area
Afterwards, we can extract the region with Numpy slicing
import cv2
image = cv2.imread('1.jpg')
original = image.copy()
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (9,9), 0)
thresh = cv2.threshold(gray,0,255,cv2.THRESH_OTSU + cv2.THRESH_BINARY)[1]
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (2,2))
opening = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel)
dilate_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (9,9))
dilate = cv2.dilate(opening, dilate_kernel, iterations=5)
cnts = cv2.findContours(dilate, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
cnts = sorted(cnts, key=cv2.contourArea, reverse=True)
for c in cnts:
x,y,w,h = cv2.boundingRect(c)
cv2.rectangle(image, (x, y), (x + w, y + h), (36,255,12), 2)
ROI = original[y:y+h, x:x+w]
break
cv2.imshow('thresh', thresh)
cv2.imshow('opening', opening)
cv2.imshow('dilate', dilate)
cv2.imshow('image', image)
cv2.imshow('ROI', ROI)
cv2.waitKey(0)
Here's my approach using NumPy's sum. Just sum the pixel values along the x and y axis individually, set up some thresholds for the minimum number of pixels describing the desired area, and obtain proper column and row indices.
Let's have a look at the following code:
import cv2
import numpy as np
from matplotlib import pyplot as plt
# Read input image; get shape
img = cv2.imread('images/UKf5Z.jpg', cv2.IMREAD_GRAYSCALE)
w, h = img.shape[0:2]
# Threshold to prevent JPG artifacts
_, img = cv2.threshold(img, 240, 255, cv2.THRESH_BINARY)
# Sum pixels along x and y axis
xSum = np.sum(img / 255, axis=0)
ySum = np.sum(img / 255, axis=1)
# Visualize curves
plt.plot(xSum)
plt.plot(ySum)
plt.show()
# Set up thresholds
xThr = 15
yThr = 15
# Find proper row indices
tmp = np.argwhere(xSum > xThr)
tmp = tmp[np.where((tmp > 20) & (tmp < w - 20))]
x1 = tmp[0]
x2 = tmp[-1]
# Find proper column indices
tmp = np.argwhere(ySum > yThr)
tmp = tmp[np.where((tmp > 20) & (tmp < h - 20))]
y1 = tmp[0]
y2 = tmp[-1]
# Visualize result
out = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
cv2.rectangle(out, (x1, y1), (x2, y2), (0, 0, 255), 4)
cv2.imshow('out', out)
cv2.waitKey(0)
The curves of the summations look like this (just for visualization purposes):
And, for visualization I just drew a red rectangle described by the found indices.
As you can see, I manually excluded some "border" area of 20 pixels, since there are some larger artifacts. Depending on the location of your desired area, this may be sufficient. Otherwise, your approach using morphological opening should be kept.
Hope that helps!
EDIT: As suggested by Mark in his answer, using mean instead of sum avoids adaptations regarding varying image dimensions. Changing the code appropriately is left to the reader. :-)
Same idea as #HansHirse's excellent answer, but I had made a diagram that I couldn't share in the comments.

Why to reshape MSER contours before detecting texts?

I am using MSER from opencv-python to detect text using the code from this stackoverflow question. Can anyone help me understand why the contour p is being reshaped to (-1, 1, 2) before computing the convex hull of the objects?
The code is as below:
import cv2
import numpy as np
#Create MSER object
mser = cv2.MSER_create()
#Your image path i-e receipt path
img = cv2.imread('/home/rafiullah/PycharmProjects/python-ocr-master/receipts/73.jpg')
#Convert to gray scale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
vis = img.copy()
#detect regions in gray scale image
regions, _ = mser.detectRegions(gray)
hulls = [cv2.convexHull(p.reshape(-1, 1, 2)) for p in regions]
cv2.polylines(vis, hulls, 1, (0, 255, 0))
cv2.imshow('img', vis)
cv2.waitKey(0)
mask = np.zeros((img.shape[0], img.shape[1], 1), dtype=np.uint8)
for contour in hulls:
cv2.drawContours(mask, [contour], -1, (255, 255, 255), -1)
#this is used to find only text regions, remaining are ignored
text_only = cv2.bitwise_and(img, img, mask=mask)
cv2.imshow("text only", text_only)
cv2.waitKey(0)
It doesn't matter if you reshape or not.
The reshaping is unnecessary. cv2.convexHull() can take either input format. The following images show that the results are the same whether the contours in regions are reshaped or not.
hulls = [cv2.convexHull(p.reshape(-1, 1, 2)) for p in regions]
hulls1 = [cv2.convexHull(p) for p in regions]
This is how the p contour changes when it is reshaped:
>>> p
array([[305, 382],
[306, 382],
[308, 380],
[309, 380]...
>>> p.reshape(-1, 1, 2)
array([[[305, 382]],
[[306, 382]],
[[308, 380]],
[[309, 380]]...

Get contours and points of an image

I have my code like this:
import numpy as np
import cv2
im = cv2.imread('snorlax.jpg')
imgray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(imgray, 127, 255, 0)
im2, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
print(contours)
cv2.drawContours(im, contours, -1, (0, 255, 0), 3)
cv2.imshow("imagen", im)
input()
The print show a list of lists that have to number every list I dont know if that are the points (x,y) of the contours and the cv2.show only showme a grey screen and doesn't show me the contours of the image.
import numpy as np
import cv2
img = cv2.imread("snorlax.jpg", cv2.IMREAD_GRAYSCALE)
canny = cv2.Canny(img, 100, 150)
cv2.imshow("Image", img)
cv2.imshow("Canny", canny)
indices = np.where(canny != [0])
coordinates = zip(indices[0], indices[1])
coordinates_list = ""
for coordinate in coordinates:
x = "'('{}, {}')', ".format(coordinate[1] / 100, -coordinate[0] / 100)
coordinates_list += x
coordinates_list = "'('{}')'".format(coordinates_list)
coordinates_list = coordinates_list.replace("'('", "{")
coordinates_list = coordinates_list.replace("')'", "}")
print(coordinates_list)
cv2.waitKey(0)
cv2.destroyAllWindows()
I use canny to resolve the problem then with the "where" function of numpy I get all the white points and zip it in a variable, the last past of the code is to get the points in a specific format to use it in an other language.

OpenCV - visualize polygonal curve(s) extracted with cv2.approxPolyDP()

I want to visualize polygonal curve(s) extracted with cv2.approxPolyDP(). Here's the image I am using:
My code attempts to isolate the main island and define and plot the contour approximation and contour hull. I have plotted the contour found in green, the approximation in red:
import numpy as np
import cv2
# load image and shrink - it's massive
img = cv2.imread('../data/UK.png')
img = cv2.resize(img, None,fx=0.25, fy=0.25, interpolation = cv2.INTER_CUBIC)
# get a blank canvas for drawing contour on and convert img to grayscale
canvas = np.zeros(img.shape, np.uint8)
img2gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
# filter out small lines between counties
kernel = np.ones((5,5),np.float32)/25
img2gray = cv2.filter2D(img2gray,-1,kernel)
# threshold the image and extract contours
ret,thresh = cv2.threshold(img2gray,250,255,cv2.THRESH_BINARY_INV)
im2,contours,hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
# find the main island (biggest area)
cnt = contours[0]
max_area = cv2.contourArea(cnt)
for cont in contours:
if cv2.contourArea(cont) > max_area:
cnt = cont
max_area = cv2.contourArea(cont)
# define main island contour approx. and hull
perimeter = cv2.arcLength(cnt,True)
epsilon = 0.01*cv2.arcLength(cnt,True)
approx = cv2.approxPolyDP(cnt,epsilon,True)
hull = cv2.convexHull(cnt)
# cv2.isContourConvex(cnt)
cv2.drawContours(canvas, cnt, -1, (0, 255, 0), 3)
cv2.drawContours(canvas, approx, -1, (0, 0, 255), 3)
## cv2.drawContours(canvas, hull, -1, (0, 0, 255), 3) # only displays a few points as well.
cv2.imshow("Contour", canvas)
k = cv2.waitKey(0)
if k == 27: # wait for ESC key to exit
cv2.destroyAllWindows()
Here are the resulting images:
The first image plots the contour in green. The second plots the approximation in red - how do I plot this approximation as a continuous closed curve?
The documentation isn't terribly clear and neither is the tutorial, but my understanding is that cv2.approxPolyDP() should define a continuous, closed curve, which I should be able to plot with cv2.drawContours(). Is that correct? If so, what am I doing wrong?
The problem is in visualization only: drawContours expects array (list in case of python) of contours, not just one numpy array (which is returned from approxPolyDP).
Solution is the following: replacing
cv2.drawContours(canvas, approx, -1, (0, 0, 255), 3)
to
cv2.drawContours(canvas, [approx], -1, (0, 0, 255), 3)
cv2.approxPolyDP()
approx = cv2.approxPolyDP(cnt, 0.03 * cv2.arcLength(cnt, True), True)

OpenCv mean returns 4 element tuple

I have a set of contours defined and filled in OpenCV, and I'm trying to use this as a mask to find the mean intensity in each ROI. I thought I could do this using the cv2.mean function with a defined mask. My code is (im2 is an image read from file):
msk = np.zeros(im2.shape, np.uint8)
cv2.bilateralFilter(im2, 5, 200, 5)
im2 = cv2.GaussianBlur(im2,(5,5),0
binImg = cv2.adaptiveThreshold(im2, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 55, -5)
contours, heir = cv2.findContours(binImg, cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
cv2.drawContours(msk, contours, -1, 255, -1)
print len(contours)
print cv2.mean(im2, mask = msk)
This returns:
3361
(155.88012076286788, 0.0, 0.0, 0.0)
I thought that I would get a mean intensity per contour, but it looks like an overall mean intensity for each channel (the image is greyscale). Are my expectations incorrect, or is my code incorrect?
Just to follow up on this (and close it out), I did resolve it by iterating over contours, and using the contours as a mask for the original image. The code is:
msk = np.zeros(im2.shape, np.uint8)
cv2.bilateralFilter(im2, 5, 200, 5)
im2 = cv2.GaussianBlur(im2,(5,5),0)
binImg = cv2.adaptiveThreshold(im2, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 55, -5)
contours, heir = cv2.findContours(binImg, cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
cv2.drawContours(msk, contours, -1, 255, -1)
for cnt in contours:
res = np.zeros(img.shape, np.uint8)
(x,y), radius = cv2.minEnclosingCircle(cnt)
ctr = (int(x), int(y))
rad = int(radius)
circ = cv2.circle(res, ctr, rad,1,-1)
print "Area: " + str(cv2.contourArea(cnt)), "Mean: " + str(float(cv2.meanStdDev(img, mask=res)[0]))
It should be noted that I'm using the meanStdDev (I did some editing and wanted to return Std Dev as well), rather than mean, but either should work for finding means. It's still not clear why mean seemed to return 4 results (for 4 channels?) on a greyscale image in the original example.

Categories

Resources