Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenCV findContours in python

I am working in python on openCV 3.0. In order to find the largest white pixel region, first of all thresholded gray image to binary image.

import cv2

import numpy as np

img = cv2.imread('graimage.png') 

img = cv2.resize(img,(400,500))

gray = img.copy()

(thresh, im_bw) = cv2.threshold(img, 0, 255, cv2.THRESH_BINARY )

derp,contours,hierarchy = cv2.findContours(im_bw,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)

cnts = max(cnts, key=cv2.contourArea)

But it shows error as follows.

cv2.error: ..../opencv/modules/imgproc/src/contours.cpp:198: error: (-210) [Start]FindContours supports only CV_8UC1 images when mode != CV_RETR_FLOODFILL otherwise supports CV_32SC1 images only in function cvStartFindContours.

like image 990
Kaira Avatar asked Jan 11 '16 08:01

Kaira


People also ask

What is the use of findcontour in OpenCV?

Contours are defined as the line joining all the points along the boundary of an image that are having the same intensity. Contours come handy in shape analysis, finding the size of the object of interest, and object detection. OpenCV has findContour() function that helps in extracting the contours from the image.

How to find centers of contours using OpenCV in Python?

In this article, we will learn how to find centers of contours using OpenCV in python. We will be using the findContours () and moments () functions. Step 1: Import the required module. Step 2: Threshold of the image. Before we go for contour detection, we have to threshold the above image which we can do using the following snippet:

How to retrieve contours from image in Python?

First one is source image, second is contour retrieval mode, third is contour approximation method and it outputs the image, 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. Contours Approximation Method –

How to use OpenCV for image analysis in Python?

For the purpose of image analysis we use the Opencv (Open Source Computer Vision Library) python library. The library name that has to be imported after installing opencv is cv2. In the below example we find the contours present in an image files. Contours help us identify the shapes present in an image.


1 Answers

It looks like this was answered in the comments, but just to mark the question as answered:

CV_8UC1 means 8-bit pixels, unsigned, and only one channel, so grayscale. It looks like you're reading it in with 3 color channels, or CV_8UC3. You can check the image type by printing img.dtype and img.shape. The dtype should be uint8, and the shape should be (#, #), indicating two dimensions. I'm guessing you'll see that shape prints (#, #, 3) for your image as-is, indicating three color channels.

As @user3515225 said, you can fix that by reading the image in as grayscale using cv2.imread('img.png', cv2.IMREAD_GRAYSCALE). That assumes you have no use for color anywhere else, though. If you want a separate grayscale copy of the image, then replace gray = img.copy() with gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY) instead.

like image 146
dirtbirb Avatar answered Sep 19 '22 07:09

dirtbirb