How can I plot/save the inner contours of an image using python opencv?
I know how can I get the largest contour, I want to save it and the inside holes, which are contours as well.
Original Image:
import numpy as np
import cv2
from matplotlib import pyplot as plt
rgb = cv2.imread('MIL_NPGERBV2.png')
grayImg = cv2.imread('MIL_NPGERBV2.png', cv2.CV_LOAD_IMAGE_GRAYSCALE)
#to apply properly contour algorithm we need convert to binary
(thresh, bwImage) = cv2.threshold(image, 128, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)
img1 = rgb.copy()
img2 = rgb.copy()
contours, hierarchy = cv2.findContours(bwImage,cv2.RETR_TREE,
cv2.CHAIN_APPROX_SIMPLE)
#show all contours
cv2.drawContours(img1, contours, -1, (0,255,0), 2)
out = np.hstack([img1])
cv2.imshow('Output', out)
cv2.waitKey(0)
cv2.destroyAllWindows()
OpenCV's findContours method can give you the internal contours if asked politely.
It's one of the mode options of cv2.findContours() :
CV_RETR_CCOMP retrieves all of the contours and organizes them into a
two-level hierarchy. At the top level, there are external boundaries
of the components. At the second level, there are boundaries of the
holes. If there is another contour inside a hole of a connected
component, it is still put at the top level.
So asking a nice
myContours = cv2.findContours(myImg,cv2.RETR_CCOMP,cv2.CHAIN_APPROX_SIMPLE)
should give you a sort of nested object array, with the first item giving the useless outlines and the rest with the insides.
I'll try to update the answer to include a MCVE later on (but only if you update the question to include one too ^^)
Related
How can I fix it?
import numpy as np
import cv2 as cv
im = cv.imread('good.jpg')
imgray = cv.cvtColor(im, cv.COLOR_BGR2GRAY)
ret, thresh = cv.threshold(imgray, 127, 255, 0)
im2, contours, hierarchy = cv.findContours(thresh, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)
Error
ValueError: not enough values to unpack (expected 3, got 2)
The problem is in your last line. To better understand it, here's a quote taken from the documentation:
See, there are three arguments in cv2.findContours() function, first one is source image, second is contour retrieval mode, third is contour approximation method. And it outputs the contours and hierarchy. Contours is a Python list of all the contours in the image. Each individual contour is a Numpy array of (x,y) coordinates of boundary points of the object.
So clearly cv2.findContours() only return two arguments (not 3). To fix it just change the last line to:
contours, hierarchy = cv.findContours(thresh, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)
I'm trying to create a script to count dots on an image (very original) using Python and OpenCV.
import numpy as np
import cv2
# Image read
img = cv2.imread("img.tif", 0)
# Denoising
denoisedImg = cv2.fastNlMeansDenoising(img);
# Threshold (binary image)
# thresh – threshold value.
# maxval – maximum value to use with the THRESH_BINARY and THRESH_BINARY_INV thresholding types.
# type – thresholding type
th, threshedImg = cv2.threshold(denoisedImg, 100, 255,cv2.THRESH_BINARY_INV|cv2.THRESH_OTSU) # src, thresh, maxval, type
# Perform morphological transformations using an erosion and dilation as basic operations
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (4,8))
morphImg = cv2.morphologyEx(threshedImg, cv2.MORPH_CLOSE, kernel)
# Find and draw contours
contours, hierarchy = cv2.findContours(morphImg, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
contoursImg = cv2.cvtColor(morphImg, cv2.COLOR_GRAY2RGB)
cv2.drawContours(contoursImg, contours, -1, (255,100,0), 3)
cv2.imwrite("image.tif", contoursImg)
print("Dots number: {}".format(len(contours)))
On the left image, the result I achieved using the code above. On the right image, what I want to achieve.
As you can see, I'm looking for a way to split dots which are a little connected into two separate dots and I was wondering if there was a "magical" function allowing me to do that.
Thanks in advance for your help.
My objective is to pick up an image, separate out the curves/contours that have a grayscale threshold below a local number (say 3), and then have rectangles around them, while writing this back onto the original image - as a way of detecting cracks on a grayscale image. Below is what I have come up with - by seeing tutorials online.
# import the necessary packages
import numpy as np
import cv2
# Load an color image in grayscale
img = cv2.imread('chest-ct-lungs.jpg',0)
ret,thresh = cv2.threshold(img,3,255,cv2.THRESH_BINARY_INV)
# Detect the contours in the image
image, contours =
cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)
# Draw all the contours
all_contour_img = cv2.drawContours(image, contours, -1, (0,255,0), 3)
cv2.imwrite('all_contour_img.png',all_contour_img)
# Use bounding rectangles
x,y,w,h = cv2.boundingRect(contours)
cv2.rectangle(all_contour_img,(x,y),(x+w,y+h),(0,255,0),2)
# Draw over original image
imwrite(uint8(double(img)+all_contour_img), 'output.png');
However, I am getting this as the output when I run it using python IDLE:
Traceback (most recent call last):
File "C:\Users\com\Desktop\python.py", line 17, in <module>
all_contour_img = cv2.drawContours(image, contours, -1, (0,255,0), 3)
TypeError: Expected cv::UMat for argument 'image'
Any inputs on where I am going wrong, as well as better practices of writing the above code - I'm a beginner.
I want this to happen :
Depending on the version of OpenCV you are using, cv2.findContours() will return a list of contours and some other stuff. All that you want is the list of contours. You can just ignore the other stuff and clean up your code by assigning those unused variables to an _.
cv2.findContours returns a LIST of contours. This is like a list of shapes. If you want to draw a bounding rectangle around each shape, you need to iterate through the list of contours.
# Import the necessary packages
import numpy as np
import cv2
# Load an color image in grayscale, threshold
img = cv2.imread('/home/stephen/Desktop/test.png')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
_, thresh = cv2.threshold(gray,3,255,cv2.THRESH_BINARY_INV)
# Detect the contours in the image
_, contours, _ = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)
# Draw all the contours
img = cv2.drawContours(img, contours, -1, (0,255,0), 1)
# Iterate through all the contours
for contour in contours:
# Find bounding rectangles
x,y,w,h = cv2.boundingRect(contour)
# Draw the rectangle
cv2.rectangle(img,(x,y),(x+w,y+h),(255,255,0),1)
# Write the image
cv2.imwrite('/home/stephen/Desktop/lines.png', img);
I'm currently working on an image where I have to find the box outer region. But I failed to find the white and black boxes regions.
input image:
https://i.imgur.com/gec9eP5.png
output image:
https://i.imgur.com/Giz1DAW.png
Update edit:
if I use HLS instead of HSV I can find 3 more box region but 2 is still missing.
here is new output:
https://i.imgur.com/eUqltKI.png
and here is my code:
import cv2
import numpy as np
img = cv2.imread("1.png")
imghsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
lower_blue = np.array([0,50,0])
upper_blue = np.array([255,255,255])
mask_blue = cv2.inRange(imghsv, lower_blue, upper_blue)
_, contours, _ = cv2.findContours(mask_blue, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
im = np.copy(img)
cv2.drawContours(im, contours, -1, (0, 255, 0), 2)
cv2.imwrite("contours_blue.png", im)
The mask you're generating with
mask_blue = cv2.inRange(imghsv, lower_blue, upper_blue)
does not include the bottom row at all, so it's impossible to detect these outlines with
_, contours, _ = cv2.findContours(mask_blue, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
You could try to work with multiple masks / thresholds to account for the different color ranges and merge the detected contours.
Color channel threshold is not the optimal solution in cases when you are dealing with objects of many different colors (that are not known in advance) and with background color that is not necessarily distinctly different from all object colors. Combination of multiple thresholds/conditions could solve the job for this particular image but this same combination can fail for slightly different input, so I think this approach is generally not too good.
I think the problem is very elementary in nature so I would recommend sticking to a simple approach. For example, if you apply Sobel operator on your image, you get result like one below. The intensity of the result is weak on some borders, so I inverted the image colors to make it better visible.
There are tons of tutorials on Sobel operator on the web so I won't go into detail here. On your input image there is no noise, so the intensity outside and within the boxes is zero. I would therefore suggest masking-out all zero values. If you do contour detection after that, you will end up with two contours per square - one will be on the inner side of the border and one on the outer side of the border. If you only want to extract contours on the outer border, see how contour hirearchy works in the OpenCV documentation. If you want to have contour exactly on the border, help yourself with outer contour and erosion.
I have the following code in Python to find contours in my image:
import cv2
im = cv2.imread('test.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)
Now I want to copy the area inside the first contour to another image, but I can't find any tutorial or example code that shows how to do that.
Here's a fully working example. It's a bit overkill in that it outputs all the contours but I think you may find a way to tweak it to your liking. Also not sure what you mean by copying, so I'll assume you just want the contours outputted to a file.
We will start with an image like so (in this case you will notice I don't need to threshold the image). The script below can be broken down into 6 major steps:
Canny filter to find the edges
cv2.findContours to keep track of our contours, note that we only need outer contours hence the cv2.RETR_EXTERNAL flag.
cv2.drawContours draws the shapes of each contour to our image
Loop through all contours and put bounding boxes around them.
Use the x,y,w,h information of our boxes to help us crop every contour
Write the cropped image to a file.
import cv2
image = cv2.imread('images/blobs1.png')
edged = cv2.Canny(image, 175, 200)
contours, hierarchy = cv2.findContours(edged, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cv2.drawContours(image, contours, -1, (0,255,0), 3)
cv2.imshow("Show contour", image)
cv2.waitKey(0)
cv2.destroyAllWindows()
for i,c in enumerate(contours):
rect = cv2.boundingRect(c)
x,y,w,h = rect
box = cv2.rectangle(image, (x,y), (x+w,y+h), (0,0,255), 2)
cropped = image[y: y+h, x: x+w]
cv2.imshow("Show Boxes", cropped)
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.imwrite("blobby"+str(i)+".png", cropped)
cv2.imshow("Show Boxes", image)
cv2.waitKey(0)
cv2.destroyAllWindows()
Not familiar with cv2.findContours but I imagine that a contour is represented by an array of points with row/column (x/y values. If this is the case and the contour is a single pixel in width then there should be two points for every row - one each on the left and right extremes of the contour.
For each row in the contour
*select* all the points in the image that are between the two contour points for that row
save those points to a new array.
As #DanMašek points out, if the points in the contour array describe a simple shape with with only the ends, corners or breakpoints represented then you would need to fill in the gaps to use the method above.
Also if the contour shape is something like a star you would need to figure out a different method for determining if an image point is inside of the contour. The method I posted is a bit naive - but might be a good starting point. For a convoluted shape like a star there might be multiple points per row of the contour but it seems like the points would come in pairs and the points you are interested in would be between the pairs.
.