Ask Your Question
0

Unable to find contours in an image

asked 2015-11-16 06:23:27 -0600

Roarer gravatar image

updated 2015-11-16 07:17:10 -0600

Hi,

I am using the following code :

import numpy as np
import cv2
import matplotlib.pyplot as plt
im = cv2.imread('shapes.png')
imgray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
image, contours, hierarchy = cv2.findContours(imgray,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)
img = cv2.drawContours(im, contours, -1, (0,255,255), 3)
plt.imshow(img)
plt.show()



lower = np.array([0, 0, 0])
upper = np.array([255, 255, 255])
shapeMask = cv2.inRange(im, lower, upper)

# find the contours in the mask
(_,cnts, _) = cv2.findContours(shapeMask.copy(), cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
print "I found %d black shapes" % (len(cnts))
cv2.imshow("Mask", shapeMask)

# loop over the contours
for c in cnts:
    # draw the contour and show it
    cv2.drawContours(image, [c], -1, (0, 255, 0), 2)
    cv2.imshow("Image", image)
    cv2.waitKey(0)

I am trying to find contours and have tried 2 different methods with two different images. Both of them dont work. Could someone pleass help me out?

image description image description

Trying watershed algortihm on this image:

import numpy as np
import cv2
from matplotlib import pyplot as plt

img = cv2.imread('IMG_1340.jpg')
b,g,r = cv2.split(img)
rgb_img = cv2.merge([r,g,b])

gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(gray,0,255,cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)

# noise removal
kernel = np.ones((2,2),np.uint8)
#opening = cv2.morphologyEx(thresh,cv2.MORPH_OPEN,kernel, iterations = 2)
closing = cv2.morphologyEx(thresh,cv2.MORPH_CLOSE,kernel, iterations = 2)

# sure background area
sure_bg = cv2.dilate(closing,kernel,iterations=3)

# Finding sure foreground area
dist_transform = cv2.distanceTransform(sure_bg,cv2.DIST_L2,3)

# Threshold
ret, sure_fg = cv2.threshold(dist_transform,0.1*dist_transform.max(),255,0)

plt.subplot(321),plt.imshow(rgb_img)
plt.title('Input Image'), plt.xticks([]), plt.yticks([])
plt.subplot(322),plt.imshow(thresh, 'gray')
plt.title("Otsu's binary threshold"), plt.xticks([]), plt.yticks([])

plt.subplot(323),plt.imshow(closing, 'gray')
plt.title("morphologyEx:Closing:2x2"), plt.xticks([]), plt.yticks([])
plt.subplot(324),plt.imshow(sure_bg, 'gray')
plt.title("Dilation"), plt.xticks([]), plt.yticks([])

plt.subplot(325),plt.imshow(dist_transform, 'gray')
plt.title("Distance Transform"), plt.xticks([]), plt.yticks([])
plt.subplot(326),plt.imshow(sure_fg, 'gray')
plt.title("Thresholding"), plt.xticks([]), plt.yticks([])

plt.tight_layout()
plt.show()

What I get

image description

edit retag flag offensive close merge delete

Comments

findContours() works over binary images. When the input is a grayscale image, non-zero pixels are treated as 1's. See docs. I guess that's your problem, you're directly working over the grayscale version of your image

LorenaGdL gravatar imageLorenaGdL ( 2015-11-16 06:47:25 -0600 )edit

Ah, okay. So is there something like findcontours for colour images? Which function would you suggest?

Roarer gravatar imageRoarer ( 2015-11-16 06:58:17 -0600 )edit

Watershed algorithm may be what you need. Take a look at this tutorial

LorenaGdL gravatar imageLorenaGdL ( 2015-11-16 07:09:53 -0600 )edit

Edited the question. PLease take a look

Roarer gravatar imageRoarer ( 2015-11-16 07:17:29 -0600 )edit

Your 2 test images are very different. While one has solid uniform colors and very defined shapes, the other is blurry and have lots of gradient info. You will need to adjust your code to work with your general case, because segmentation is not as easy as it seems

LorenaGdL gravatar imageLorenaGdL ( 2015-11-16 08:50:31 -0600 )edit

When you say adjust code, do you mean to say i have to find the findcontours function and implement it on my own? Any other suggestions?

Roarer gravatar imageRoarer ( 2015-11-16 09:02:27 -0600 )edit

What are you trying exactly?

LorenaGdL gravatar imageLorenaGdL ( 2015-11-16 09:16:26 -0600 )edit

I have an image with different colours in a square. I am trying to extract this square colour information. However to do that, first I need to identify squares which doesn't seem to be working

Roarer gravatar imageRoarer ( 2015-11-16 09:26:11 -0600 )edit

It will work once you find the right way to binarize your images. As I said, segmentation has not a general solution, and you will need to put restrictions based on your input images

LorenaGdL gravatar imageLorenaGdL ( 2015-11-16 09:33:24 -0600 )edit

If you can post it as an answer I can accept it

Roarer gravatar imageRoarer ( 2015-11-17 00:54:49 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2015-11-16 09:37:19 -0600

Roarer gravatar image

Okay. Will do. Thanks for the help

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2015-11-16 06:21:35 -0600

Seen: 862 times

Last updated: Nov 16 '15