Image Segmentation using OpenCV - Extracting specific Areas of an image (2023)

In the previous tutorials, we have used OpenCV for basic image processing and done some advance image editing operations. As we know, OpenCV is Open Source Commuter Vision Library which has C++, Python and Java interfaces and supports Windows, Linux, Mac OS, iOS and Android. So it can be easily installed in Raspberry Pi with Python and Linux environment. And Raspberry Pi with OpenCV and attached camera can be used to create many real-time image processing applications like Face detection, face lock, object tracking, car number plate detection, Home security system etc. In this tutorial we will learn that how to do image segmentation using OpenCV. The operations we are going to perform are listed below:

  • Segmentation and contours
  • Hierarchy and retrieval mode
  • Approximating contours and finding their convex hull
  • Conex Hull
  • Matching Contour
  • Identifying Shapes (circle, rectangle, triangle, square, star)
  • Line detection
  • Blob detection
  • Filtering the blobs – counting circles and ellipses

1. Segmentation and contours

Image segmentation is a process by which we partition images into different regions. Whereas the contours are the continuous lines or curves that bound or cover the full boundary of an object in an image. And, here we will use image segmentation technique called contours to extract the parts of an image.

Also contours are very much important in

  • Object detection
  • Shape analysis

And they have very much broad field of application from the real world image analysis to medical image analysis such as in MRI’s

Let’s know how to implement contours in opencv, by extracting contours of squares.

import cv2import numpy as np

Let’s load a simple image with 3 black squares

image=cv2.imread('squares.jpg')cv2.imshow('input image',image)cv2.waitKey(0)

Grayscale

gray=cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)

Find canny edges

edged=cv2.Canny(gray,30,200)cv2.imshow('canny edges',edged)cv2.waitKey(0)

Finding contours

#use a copy of your image, e.g. - edged.copy(), since finding contours alter the image#we have to add _, before the contours as an empty argument due to upgrade of the OpenCV version_, contours, hierarchy=cv2.findContours(edged,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)cv2.imshow('canny edges after contouring', edged)cv2.waitKey(0)

Printing the contour file to know what contours comprises of

print(contours)print('Numbers of contours found=' + str(len(contours)))

Draw all contours

#use -1 as the 3rd parameter to draw all the contourscv2.drawContours(image,contours,-1,(0,255,0),3)cv2.imshow('contours',image)cv2.waitKey(0)cv2.destroyAllWindows()

Image Segmentation using OpenCV - Extracting specific Areas of an image (1)

Console Output- [array([[[368, 157]],

[[367, 158]],

[[366, 159]],

...,

[[371, 157]],

[[370, 157]],

[[369, 157]]],

dtype=int32),

array([[[520, 63]],

[[519, 64]],

[[518, 65]],

...,

[[523, 63]],

[[522, 63]],

[[521, 63]]], dtype=int32),array([[[16, 19]],

[[15, 20]],

[[15, 21]],

...,

[[19, 19]],

[[18, 19]],

[[17, 19]]], dtype=int32)]

Numbers of contours found=3. So we have found a total of three contours.

Now, in the above code we had also printed the contour file using [print(contours)], this file tells how these contours looks like, as printed in above console output.

(Video) OpenCV Python Tutorial | Extracting Selective Image - Image Segmentation | OneTouchBI

In the above console output we have a matrix which looks like coordinates of x, y points. OpenCV stores contours in a lists of lists. We can simply show the above console output as follows:

CONTOUR 1 CONTOUR 2 CONTOUR 3

[array([[[368, 157]], array([[[520, 63]], array([[[16, 19]],

[[367, 158]], [[519, 64]], [[15, 20]],

[[366, 159]], [[518, 65]], [[15, 21]],

..., ..., ...,

[[371, 157]], [[523, 63]], [[19, 19]],

[[370, 157]], [[522, 63]], [[18, 19]],

[[369, 157]]], dtype=int32), [[521, 63]]], dtype=int32), [[17, 19]]], dtype=int32)]

Now, as we use the length function on contour file, we get the length equal to 3, it means there were three lists of lists in that file, i.e. three contours.

Now, imagine CONTOUR 1 is the first element in that array and that list contains list of all the coordinates and these coordinates are the points along the contours that we just saw, as the green rectangular boxes.

There are different methods to store these coordinates and these are called approximation methods, basically approximation methods are of two types

  • cv2.CHAIN_APPROX_NONE
  • cv2.CHAIN_APPROX_SIMPLE

cv2.CHAIN_APPROX_NONE stores all the boundary point, but we don’t necessarily need all the boundary points, if the point forms a straight line, we only need the start point and ending point on that line.

cv2.CHAIN_APPROX_SIMPLE instead only provides the start and end points of the bounding contours, the result is much more efficient storage of contour information.

_, contours,hierarchy=cv2.findContours(edged,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)

In the above code cv2.RETR_EXTERNAL is the retrieval mode while the cv2.CHAIN_APPROX_NONE is

the approximation method.

So we have learned about contours and approximation method, now let’s explore hierarchy and retrieval mode.

2. Hierarchy and Retrieval Mode

Retrieval mode defines the hierarchy in contours like sub contours, or external contour or all the contours.

Now there are four retrieval modes sorted on the hierarchy types.

cv2.RETR_LIST – retrieves all the contours.

cv2.RETR_EXTERNAL – retrieves external or outer contours only.

cv2.RETR_CCOMP – retrieves all in a 2-level hierarchy.

cv2.RETR_TREE – retrieves all in a full hierarchy.

Hierarchy is stored in the following format [Next, Previous, First child, parent]

Now let’s illustrate the difference between the first two retrieval modes, cv2.RETR_LIST and cv2.RETR_EXTERNAL.

import cv2import numpy as np

Lets load a simple image with 3 black squares

image=cv2.imread('square donut.jpg')cv2.imshow('input image',image)cv2.waitKey(0)

Grayscale

gray=cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)

Find Canny Edges

edged=cv2.Canny(gray,30,200)cv2.imshow('canny edges',edged)cv2.waitKey(0)

Finding Contours

#use a copy of your image, e.g. - edged.copy(), since finding contours alter the image#we have to add _, before the contours as an empty argument due to upgrade of the open cv version_, contours,hierarchy=cv2.findContours(edged,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)cv2.imshow('canny edges after contouring', edged)cv2.waitKey(0)

Printing the contour file to know what contours comprises of.

print(contours)print('Numbers of contours found=' + str(len(contours)))

Draw all contours

#use -1 as the 3rd parameter to draw all the contourscv2.drawContours(image,contours,-1,(0,255,0),3)cv2.imshow('contours',image)cv2.waitKey(0)cv2.destroyAllWindows

Image Segmentation using OpenCV - Extracting specific Areas of an image (2)

Now let’s change the retrieval mode from external to list

import cv2import numpy as np

Lets load a simple image with 3 black squares

image=cv2.imread('square donut.jpg')cv2.imshow('input image',image)cv2.waitKey(0)

Grayscale

(Video) Extracting Bounding Box from Binary Segmentation Mask | UNET | Image Segmentation | Python | OpenCV

gray=cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)

Find canny edges

edged=cv2.Canny(gray,30,200)cv2.imshow('canny edges',edged)cv2.waitKey(0)

Finding contours

#use a copy of your image, e.g. - edged.copy(), since finding contours alter the image#we have to add _, before the contours as an empty argument due to upgrade of the open cv version_, contours,hierarchy=cv2.findContours(edged,cv2.RETR_LIST,cv2.CHAIN_APPROX_NONE)cv2.imshow('canny edges after contouring', edged)cv2.waitKey(0)

Printing the contour file to know what contours comprises of.

print(contours)print('Numbers of contours found=' + str(len(contours)))

Draw all contours

#use -1 as the 3rd parameter to draw all the contourscv2.drawContours(image,contours,-1,(0,255,0),3)cv2.imshow('contours',image)cv2.waitKey(0)cv2.destroyAllWindows()

Image Segmentation using OpenCV - Extracting specific Areas of an image (3)

So through the demonstration of above codes we could clearly see the difference between the cv2.RETR_LIST and cv2.RETR_EXTERNNAL, in cv2.RETR_EXTERNNAL only the outer contours are being taken into account while the inner contours are being ignored.

While in cv2.RETR_LIST inner contours are also being taken into account.

3. Approximating Contours and Finding their Convex hull

In approximating contours, a contour shape is approximated over another contour shape, which may be not that much similar to the first contour shape.

For approximation we use approxPolyDP function of openCV which is explained below

cv2.approxPolyDP(contour, approximation accuracy, closed)

Parameters:

  • Contour – is the individual contour we wish to approximate.
  • Approximation Accuracy- important parameter in determining the accuracy of approximation, small value give precise approximation, large values gives more generic information. A good thumb rule is less than 5% of contour perimeter.
  • Closed – a Boolean value that states whether the approximate contour could be open or closed.

Let’s try to approximate a simple figure of a house

import numpy as npimport cv2

Load the image and keep a copy

image=cv2.imread('house.jpg')orig_image=image.copy()cv2.imshow('original image',orig_image)cv2.waitKey(0)

Grayscale and binarize the image

gray=cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)ret, thresh=cv2.threshold(gray,127,255,cv2.THRESH_BINARY_INV)

Find Contours

_, contours, hierarchy=cv2.findContours(thresh.copy(),cv2.RETR_LIST,cv2.CHAIN_APPROX_NONE)

Iterate through each contour and compute their bounding rectangle

for c in contours: x,y,w,h=cv2.boundingRect(c) cv2.rectangle(orig_image,(x,y),(x+w,y+h),(0,0,255),2) cv2.imshow('Bounding rect',orig_image)cv2.waitKey(0)

Iterate through each contour and compute the approx contour

for c in contours:

  #calculate accuracy as a percent of contour perimeter accuracy=0.03*cv2.arcLength(c,True) approx=cv2.approxPolyDP(c,accuracy,True) cv2.drawContours(image,[approx],0,(0,255,0),2) cv2.imshow('Approx polyDP', image)cv2.waitKey(0)cv2.destroyAllWindows()

Image Segmentation using OpenCV - Extracting specific Areas of an image (4)

4. Convex Hull

Convex hull is basically the outer edges, represented by drawing lines over a given figure.

It could be the smallest polygon that can fit around the object itself.

import cv2import numpy as npimage=cv2.imread('star.jpg')gray=cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)cv2.imshow('original image',image)cv2.waitKey(0)

Threshold the image

ret, thresh=cv2.threshold(gray,176,255,0)

Find contours

_, contours, hierarchy=cv2.findContours(thresh.copy(),cv2.RETR_LIST,cv2.CHAIN_APPROX_NONE)

Sort the contours by area and then remove the largest frame contour

n=len(contours)-1contours=sorted(contours,key=cv2.contourArea,reverse=False)[:n]

Iterate through the contours and draw convex hull

for c in contours:

  hull=cv2.convexHull(c) cv2.drawContours(image,[hull],0,(0,255,0),2) cv2.imshow('convex hull',image) cv2.waitKey(0) cv2.destroyAllWindows()

5. Matching Contour by shapes

cv2.matchShapes(contour template, contour method, method parameter)

Output – match value(lower value means a closer match)

contour template – This is our reference contour that we are trying to find in a new image.

contour – The individual contour we are checking against.

Method – Type of contour matching (1,2,3).

method parameter – leave alone as 0.0 (not utilized in python opencv)

import cv2import numpy as np

Load the shape template or reference image

template= cv2.imread('star.jpg',0)cv2.imshow('template',template)cv2.waitKey(0)

Load the target image with the shapes we are trying to match

target=cv2.imread('shapestomatch.jpg')gray=cv2.cvtColor(target,cv2.COLOR_BGR2GRAY)

Threshold both the images first before using cv2.findContours

ret,thresh1=cv2.threshold(template,127,255,0)ret,thresh2=cv2.threshold(gray,127,255,0)

Find contours in template

_,contours,hierarhy=cv2.findContours(thresh1,cv2.RETR_CCOMP,cv2.CHAIN_APPROX_SIMPLE)#we need to sort the contours by area so we can remove the largest contour which is

Image outline

(Video) Extract some Image Properties using OpenCV (Python )

sorted_contours=sorted(contours, key=cv2.contourArea, reverse=True)#we extract the second largest contour which will be our template contourtempelate_contour=contours[1]#extract the contours from the second target image_,contours,hierarchy=cv2.findContours(thresh2,cv2.RETR_CCOMP,cv2.CHAIN_APPROX_SIMPLE)for c in contours: #iterate through each contour in the target image and use cv2.matchShape to compare the contour shape match=cv2.matchShapes(tempelate_contour,c,1,0.0) print("match") #if match value is less than 0.15 if match<0.16: closest_contour=c else: closest_contour=[] cv2.drawContours(target,[closest_contour],-1,(0,255,0),3)cv2.imshow('output',target)cv2.waitKey(0)cv2.destroyAllWindows()

Image Segmentation using OpenCV - Extracting specific Areas of an image (5)

Console Output –

0.16818605122199104

0.19946910256158912

0.18949760627309664

0.11101058276281539

There are three different method with different mathematics function, we can experiment with each method by just replacing cv2.matchShapes(tempelate_contour,c,1,0.0) method values which varies from 1,2 and 3, for each value you will get different match values in console output.

6. Identifying Shapes (circle, rectangle, triangle, square, star)

OpenCV can also be used for detecting different types of shapes automatically from the image. By using below code we will be able to detect circle, rectangle, triangle, square and stars from the image.

import cv2import numpy as np

Load and then gray scale images

image=cv2.imread('shapes.jpg')gray=cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)cv2.imshow('identifying shapes',image)cv2.waitKey(0)ret, thresh=cv2.threshold(gray,127,255,1)

Extract contours

_,contours,hierarchy=cv2.findContours(thresh.copy(),cv2.RETR_LIST,cv2.CHAIN_APPROX_NONE)

For cnt in contours:

 Get approximate polygons approx = cv2.approxPolyDP(cnt,0.01*cv2.arcLength(cnt,True),True) if len(approx)==3: shape_name="Triangle" cv2.drawContours(image,[cnt],0,(0,255,0),-1)

find contour center to place text at the center

 M=cv2.moments(cnt) cx=int(M['m10']/M['m00']) cy=int(M['m01']/M['m00']) cv2.putText(image,shape_name,(cx-50,cy),cv2.FONT_HERSHEY_SIMPLEX,1,(0,0,0),1) elif len(approx)==4: x,y,w,h=cv2.boundingRect(cnt) M=cv2.moments(cnt) cx=int(M['m10']/M['m00']) cy=int(M['m01']/M['m00'])

Check to see if that four sided polygon is square or rectangle

 #cv2.boundingRect return the left width and height in pixels, starting from the top #left corner, for square it would be roughly same  if abs(w-h) <= 3: shape_name="square" #find contour center to place text at center cv2.drawContours(image,[cnt],0,(0,125,255),-1) cv2.putText(image,shape_name,(cx-50,cy),cv2.FONT_HERSHEY_SIMPLEX,1,(0,0,0),1)  else: shape_name="Reactangle" #find contour center to place text at center cv2.drawContours(image,[cnt],0,(0,0,255),-1) M=cv2.moments(cnt) cx=int(M['m10']/M['m00']) cy=int(M['m01']/M['m00']) cv2.putText(image,shape_name,(cx-50,cy),cv2.FONT_HERSHEY_SIMPLEX,1,(0,0,0),1) elif len(approx)==10: shape_name='star' cv2.drawContours(image,[cnt],0,(255,255,0),-1) M=cv2.moments(cnt) cx=int(M['m10']/M['m00']) cy=int(M['m01']/M['m00']) cv2.putText(image,shape_name,(cx-50,cy),cv2.FONT_HERSHEY_SIMPLEX,1,(0,0,0),1) elif len(approx)>=15: shape_name='circle' cv2.drawContours(image,[cnt],0,(0,255,255),-1) M=cv2.moments(cnt) cx=int(M['m10']/M['m00']) cy=int(M['m01']/M['m00']) cv2.putText(image,shape_name,(cx-50,cy),cv2.FONT_HERSHEY_SIMPLEX,1,(0,0,0),1) cv2.imshow('identifying shapes', image) cv2.waitKey(0)cv2.destroyAllWindows()

Image Segmentation using OpenCV - Extracting specific Areas of an image (6)

7. Line Detection

Line detection is very much important concept in OpenCV, and has a promising use in the real world. Autonomous cars use line detection algorithms for the detection of lanes and roads.

In line detection we will deal with two algorithms,

  • Hough Line Algorithm
  • Probalistic Hough Line Algorithm.

Image Segmentation using OpenCV - Extracting specific Areas of an image (7)

You may have remembered the representation of line from high school mathematics with the equation, y=mx+c.

However, in OpenCV line is represented by another way

Image Segmentation using OpenCV - Extracting specific Areas of an image (8)

The equation above ρ=xcosӨ +ysincosӨ is the OpenCV representation of the line, wherein ρ is the perpendicular distance of line from origin and Ө is the angle formed by the normal of this line to the origin (measured in radians, wherein 1pi radians/180 = 1 degree).

The OpenCV function for the detection of line is given as

cv2.HoughLines(binarized image, ρ accuracy, Ө accuracy, threshold), wherein threshold is minimum vote for it to be considered a line.

Now let’s detect lines for a box image with the help of Hough line function of opencv.

import cv2import numpy as npimage=cv2.imread('box.jpg')

Grayscale and canny edges extracted

gray=cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)edges=cv2.Canny(gray,100,170,apertureSize=3)

Run Hough lines using rho accuracy of 1 pixel

#theta accuracy of (np.pi / 180) which is 1 degree#line threshold is set to 240(number of points on line)lines=cv2.HoughLines(edges, 1, np.pi/180, 240)#we iterate through each line and convert into the format#required by cv2.lines(i.e. requiring end points)for i in range(0,len(lines)):  for rho, theta in lines[i]:  a=np.cos(theta) b=np.sin(theta) x0=a*rho y0=b*rho x1=int(x0+1000*(-b)) y1=int(y0+1000*(a)) x2=int(x0-1000*(-b)) y2=int(y0-1000*(a)) cv2.line(image,(x1,y1),(x2,y2),(0,255,0),2)cv2.imshow('hough lines',image)cv2.waitKey(0)cv2.destroyAllWindows()

Now let’s repeat above line detection with other algorithm of probabilistic Hough line.

The idea behind probabilistic Hough line is to take a random subset of points sufficient enough for line detection.

The OpenCV function for probabilistic Hough line is represented as cv2.HoughLinesP(binarized image, ρ accuracy, Ө accuracy, threshold, minimum line length, max line gap)

Now let’s detect box lines with the help of probabilistic Hough lines.

import cv2import numpy as np

Grayscale and canny edges Extracted

image=cv2.imread('box.jpg')gray=cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)edges=cv2.Canny(gray,50,150,apertureSize=3)#again we use the same rho and theta accuracies#however, we specify a minimum vote(pts along line) of 100#and min line length of 5 pixels and max gap between the lines of 10 pixelslines=cv2.HoughLinesP(edges,1,np.pi/180,100,100,10)for i in range(0,len(lines)): for x1,y1,x2,y2 in lines[i]: cv2.line(image,(x1,y1),(x2,y2),(0,255,0),3)cv2.imshow('probalistic hough lines',image)cv2.waitKey(0)cv2.destroyAllWindows

Image Segmentation using OpenCV - Extracting specific Areas of an image (9)

8. Blob detection

Blobs can be described as a group of connected pixels that all share a common property. The method to use OpenCV blob detector is described through this flow chart.

Image Segmentation using OpenCV - Extracting specific Areas of an image (10)

For drawing the key points we use cv2.drawKeypoints which takes the following arguments.

(Video) Image Segmentation, Semantic Segmentation, Instance Segmentation, and Panoptic Segmentation

cv2.drawKeypoints(input image,keypoints,blank_output_array,color,flags)

where in the flags could be

cv2.DRAW_MATCHES_FLAGS_DEFAULT

cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS

cv2.DRAW_MATCHES_FLAGS_DRAW_OVER_OUTIMG

cv2.DRAW_MATCHES_FLAGS_NOT_DRAW_SINGLE_POINTS

and blank here is pretty much nothing but one by one matrix of zeros

Now let’s perform the blob detection on an image of sunflowers, where the blobs would be the central parts of the flower as they are common among all the flowers.

import cv2import numpy as npimage=cv2.imread('Sunflowers.jpg',cv2.IMREAD_GRAYSCALE)

Set up detector with default parameters

detector=cv2.SimpleBlobDetector_create()

Detect blobs

keypoints= detector.detect(image)

Draw detected blobs as red circles

#cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS ensure the#size of circle corresponds to the size of blobblank=np.zeros((1,1))blobs=cv2.drawKeypoints(image,keypoints,blank,(0,255,255),cv2.DRAW_MATCHES_FLAGS_DEFAULT)

Show keypoints

cv2.imshow('blobs',blobs)cv2.waitKey(0)cv2.destroyAllWindows()

Image Segmentation using OpenCV - Extracting specific Areas of an image (11)

Even though the code works fine but some of the blobs are missed due to uneven sizes of the flowers as the flowers in the front are big as compared to the flowers at the end.

9. Filtering the Blobs – Counting Circles and Ellipses

We can use parameters for filtering the blobs according to their shape, size and color. For using parameters with blob detector we use the OpenCV’s function

cv2.SimpleBlobDetector_Params()

We will see filtering the blobs by mainly these four parameters listed below:

Area

params.filterByArea=True/Falseparams.minArea=pixelsparams.maxArea=pixels

Circularity

params.filterByCircularity=True/Falseparams.minCircularity= 1 being perfect, 0 being opposite

Convexity - Area of blob/area of convex hull

params.filterByConvexity= True/Falseparams.minConvexity=Area

Image Segmentation using OpenCV - Extracting specific Areas of an image (12)

Inertia

params.filterByInertia=True/False params.minInertiaRatio=0.01

Image Segmentation using OpenCV - Extracting specific Areas of an image (13)

Now let’s try to filter blobs by above mentioned parameters

import cv2import numpy as npimage=cv2.imread('blobs.jpg')cv2.imshow('original image', image)cv2.waitKey(0)

Initialize the detector using default parameters

detector=cv2.SimpleBlobDetector_create()

Detect blobs

keypoints=detector.detect(image)

Draw blobs on our image as red circles

blank=np.zeros((1,1))blobs=cv2.drawKeypoints(image,keypoints,blank,(0,0,255),cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)number_of_blobs=len(keypoints)text="total no of blobs"+str(len(keypoints))cv2.putText(blobs,text,(20,550),cv2.FONT_HERSHEY_SIMPLEX,1,(100,0,255),2)

Display image with blob keypoints

cv2.imshow('blob using default parameters',blobs)cv2.waitKey(0)

Set our filtering parameters

#initialize parameter setting using cv2.SimpleBlobDetectorparams=cv2.SimpleBlobDetector_Params()

Set area filtering parameters

params.filterByArea=Trueparams.minArea=100

Set circularity filtering parameters

params.filterByCircularity=Trueparams.minCircularity=0.9

Set convexity filtering parameter

params.filterByConvexity=Falseparams.minConvexity=0.2

Set inertia filtering parameter

params.filterByInertia=Trueparams.minInertiaRatio=0.01

Create detector with parameter

detector=cv2.SimpleBlobDetector_create(params)

Detect blobs

keypoints=detector.detect(image)

Draw blobs on the images as red circles

blank=np.zeros((1,1))blobs=cv2.drawKeypoints(image,keypoints,blank,(0,255,0),cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)number_of_blobs=len(keypoints)text="total no of circular blobs"+str(len(keypoints))cv2.putText(blobs,text,(20,550),cv2.FONT_HERSHEY_SIMPLEX,1,(0,100,255),2)

Show blobs

(Video) 51 - Image Segmentation using K-means

cv2.imshow('filtering circular blobs',blobs)cv2.waitKey(0)cv2.destroyAllWindows()

Image Segmentation using OpenCV - Extracting specific Areas of an image (14)

So this is how Image segmentation can be done in Python-OpenCV. To get good understating of computer vision and OpenCV, go through previous articles (Getting started with Python OpenCVandImage Manipulations in Python OpenCVand you will be able to make something cool with Computer Vision.

FAQs

How do we pick up a piece of image or a region of interest in OpenCV using Python? ›

Python OpenCV – selectroi() Function

With this method, we can select a range of interest in an image manually by selecting the area on the image. Parameter: window_name: name of the window where selection process will be shown. source image: image to select a ROI.

What is region extraction in image processing? ›

Global thresholding is applied to get transition regions. Further, it undergoes morphological thinning and region filling operation to extract the object regions. Finally, the objects are extracted using the object regions. The proposed method is compared with different image segmentation methods.

What is region based segmentation in image processing? ›

Region-Based Segmentation

The similarity between pixels can be in terms of intensity, color, etc. In this type of segmentation, some predefined rules are present which have to be obeyed by a pixel in order to be classified into similar pixel regions.

How do I extract specific data from an image in Python? ›

Extract text from a single image using Python
  1. from PIL import Image.
  2. from pytesseract import pytesseract.
  3. img = Image. open(path_to_image)
  4. text = pytesseract. image_to_string(img)
  5. print(text)
12 May 2022

How do I extract the contents of an image? ›

It uses optical character recognition (OCR) technology to get the text from images.
...
How can I extract text from an image for free?
  1. Go to imagetotext.info (Free).
  2. Upload or drag and drop your image.
  3. Click the Submit button.
  4. Copy the text or save the text file on your computer.

How do you choose Region of Interest? ›

Procedure. Right-click in the image and select New ROI for and then select People or Vehicles. A default rectangular-shaped region is added to the image window in the corresponding color. Then, click each vertex of the polygon that defines the ROI.

How do I create a region of interest in OpenCV? ›

Selecting Single ROI bounding Box in OpenCV [python].
  1. Import the necessary libraries. import cv2. ...
  2. Read the image by using “imread” function. ...
  3. Pass the image in “SelectROI” function. ...
  4. save the selected rectangle point (roi) in a variable. ...
  5. Use the rectangle points to crop. ...
  6. Display the Cropped ROI. ...
  7. Save the cropped ROI.

How do you extract region of interest from a video in Python? ›

Essentially the idea is to use cv2. setMouseCallback() and event handlers to detect if the mouse has been clicked or released. For this implementation, you can extract coordinates by holding down the left mouse button and dragging to select the desired ROI. You can reset the ROI using the right mouse button.

What are the 3 types of extraction? ›

The three most common types of extractions are: liquid/liquid, liquid/solid, and acid/base (also known as a chemically active extraction).

What are the two types of extraction processes? ›

There are two types of extraction, liquid-liquid extraction also known as solvent extraction as well as solid-liquid extraction. Both extraction types are based on the same principle, the separation of compounds, based on their relative solubilities in two different immiscible liquids or solid matter compound.

What are the three types of feature extraction methods? ›

Autoencoders, wavelet scattering, and deep neural networks are commonly used to extract features and reduce dimensionality of the data.

What is the best image segmentation method? ›

Edge-Based Segmentation

Edge-based segmentation is one of the most popular implementations of segmentation in image processing. It focuses on identifying the edges of different objects in an image.

What is the main method for region segmentation? ›

Region-Based Segmentation

In this segmentation, we grow regions by recursively including the neighboring pixels that are similar and connected to the seed pixel. We use similarity measures such as differences in gray levels for regions with homogeneous gray levels.

What are the two approaches to segmentation in image processing? ›

The splitting and merging based segmentation methods use two basic techniques done together in conjunction – region splitting and region merging – for segmenting an image.

Which tool is used to extract a portion of an image? ›

Answer: the model tool is used to extract a part of a picture as 3D model.

How do you extract a particular data from a file using Python? ›

How to extract specific portions of a text file using Python
  1. Make sure you're using Python 3.
  2. Reading data from a text file.
  3. Using "with open"
  4. Reading text files line-by-line.
  5. Storing text data in a variable.
  6. Searching text for a substring.
  7. Incorporating regular expressions.
  8. Putting it all together.
30 Jun 2020

How do I extract a specific part of a string in Python? ›

Getting a substring of a string is extracting a part of a string from a string object. It is also called a Slicing operation. You can get substring of a string in python using the str[0:n] option.

How do I extract the contents of a file? ›

To unzip a single file or folder, open the zipped folder, then drag the file or folder from the zipped folder to a new location. To unzip all the contents of the zipped folder, press and hold (or right-click) the folder, select Extract All, and then follow the instructions.

Can we extract text out from image? ›

There are programs that use Optical Character Recognition (OCR) to analyze the letters and words in an image and then convert them to text. There are a number of reasons why you might want to use OCR technology to copy text from an image or PDF.

What is extracting text from an image? ›

The text extractor will allow you to extract text from any image. You may upload an image or document (. pdf) and the tool will pull text from the image. Once extracted, you can copy to your clipboard with one click.

How do you define region of interest? ›

A region of interest (often abbreviated ROI) is a sample within a data set identified for a particular purpose. The concept of a ROI is commonly used in many application areas. For example, in medical imaging, the boundaries of a tumor may be defined on an image or in a volume, for the purpose of measuring its size.

When should we use region of interest? ›

ROI is most commonly used for medical imaging, e.g. as a particular portion of a 2D, 3D or 4D image that is of concern during a diagnosis or of interest during research.

What is region of interest in object detection? ›

Region of interest pooling (also known as RoI pooling) is an operation widely used in object detection tasks using convolutional neural networks. For example, to detect multiple cars and pedestrians in a single image.

What is region of interest OpenCV? ›

Sometimes, a processing function needs to be applied only to a portion of an image. OpenCV incorporates an elegant and simple mechanism to define a subregion in an image and manipulate it as a regular image.

What is segmentation in OpenCV? ›

Steps Involved in Image Segmentation

Import the libraries. Reading the sample image on which we will be performing all the operations. Creating the function that will draw the bounding box. Explaining the GrabCut algorithm. The main function is to run the complete process all at once.

How do you select an area in Python? ›

1 Answer
  1. On mouse button down, store coords as first edge.
  2. On mouse move, update the rectangle selection with coords as second edge.
  3. On mouse button up, store the second edge.
  4. Process the capture.

How do I extract a specific item from a list in Python? ›

6 Easy Ways to Extract Elements From Python Lists
  1. Method 1: Use Slicing.
  2. Method 2: Use List Index.
  3. Method 3: Use List Comprehension.
  4. Method 4: Use List Comprehension with condition.
  5. Method 5: Use enumerate()
  6. Method 6: Use NumPy array()

How do you select area within a plot Python and extract the data within the area? ›

How to select area within a plot (Python) and extract the data within the area
  1. Plot the whole 10 seconds of data.
  2. Mark the relevant part of the signals for example with a bounding box.
  3. Extract the data within the bounding box after closing the plot window or pushing a button.
  4. Go back to 1. and take new data.
8 Jul 2017

What is ROI extraction? ›

Extraction of ROI (Region-Of-Interest) in dermatosis images can be used in content-based image retrieval (CBIR). Image segmentation takes an important part in it. And the performance of the segmentation algorithm directly influences the efficiency of the ROI extraction results.

What are the 4 steps of the extraction process? ›

The extraction of natural products progresses through the following stages: (1) the solvent penetrates into the solid matrix; (2) the solute dissolves in the solvents; (3) the solute is diffused out of the solid matrix; (4) the extracted solutes are collected.

What are different methods of extract? ›

In general, extraction procedures include maceration, digestion, decoction, infusion, percolation, Soxhlet extraction, superficial extraction, ultrasound-assisted, and microwave-assisted extractions.

How many types of extractions are there? ›

There are three common types of tooth extractions: simple extractions, impacted tooth extractions, and the removal of tooth roots. Before we explain each of these, let's look at the typical reasons why you might need to have a tooth removed, including: Trauma to the tooth.

Which type of extraction is more efficient? ›

Multiple extractions are more efficient than a single extraction with the same volume of solvent.

What is the difference between single extraction and multiple extraction? ›

In a multiple extraction of an aqueous layer, the first extraction is procedurally identical to a single extraction. In the second extraction, the aqueous layer from the first extraction is returned to the separatory funnel (Figure 4.16b), with the goal of extracting additional compound.

What is the difference between segmentation and feature extraction? ›

Image segmentation is a method which can be used to understand images and extract information or objects. It is the first step in image analysis. Feature extraction in image processing is a technique of redefining a large set of redundant data into a set of features (or feature vector) of reduced dimension.

Which feature extraction technique is best? ›

PCA is one of the most used linear dimensionality reduction technique. When using PCA, we take as input our original data and try to find a combination of the input features which can best summarize the original data distribution so that to reduce its original dimensions.

What are the common methods of feature extraction? ›

The most common linear methods for feature extraction are Principal Component Analysis (PCA) and Linear Discriminant Analysis (LDA). PCA uses an orthogonal transformation to convert data into a lower-dimensional space while maximizing the variance of the data.

Which algorithm is used for image segmentation? ›

Threshold segmentation is the simplest method of image segmentation and also one of the most com- mon parallel segmentation methods. It is a common segmentation algorithm which directly divides the im- age gray scale information processing based on the gray value of different targets.

What is image segmentation with example? ›

For example, a common application of image segmentation in medical imaging is to detect and label pixels in an image or voxels of a 3D volume that represent a tumor in a patient's brain or other organs.

What are four different types of image processing methods? ›

Common image processing include image enhancement, restoration, encoding, and compression.

What are the different segmentation methods? ›

Five ways to segment markets include demographic, psychographic, behavioral, geographic, and firmographic segmentation.

What is the basic method of segmentation? ›

Segmentation is a memory management technique which divides the program from the user's view of memory. That means the program is divided into modules/segments, unlike paging in which the program was divided into different pages, and those pages may or may not be loaded into the memory simultaneously.

What are segmenting methods? ›

There are four main customer segmentation models that should form the focus of any marketing plan. For example, the four types of segmentation are Demographic, Psychographic Geographic, and Behavioral. These are common examples of how businesses can segment their market by gender, age, lifestyle etc.

What are the 3 main types of segmentation? ›

Three Types of Segmentation and How to Use Them
  • Psychographic Segmentation. This method of segmentation addresses the consumer's values, beliefs, perceptions, attitudes, interests and behaviors. ...
  • Demographic Segmentation. ...
  • Geographic Segmentation.
28 Jun 2019

What are the 4 segmentation process? ›

There are four key types of market segmentation that you should be aware of, which include demographic, geographic, psychographic, and behavioral segmentations. It's important to understand what these four segmentations are if you want your company to garner lasting success.

What are the 3 segmentation strategies? ›

Segmentation can be approached in three main ways: firmographic, behavioural and needs-based.

How do you select region of interest in ImageJ? ›

To create a new ROI set, select Add ROI Set . If the table contains multiple ROI sets, use the drop-down box at the top of the panel to choose between them. Once you have selected an ROI set to edit, use the ordinary ROI tools in the ImageJ toolbar (square, circle, polygon, line, and point) to draw ROIs on the image.

How do you calculate region of interest? ›

Computation of intensity statistics for ROIs involves multiplying pixel intensities by the area of pixels that lie with the outline of the ROI.
...
If extended statistics are shown, then the following statistics are also computed:
  1. Median intensity.
  2. Perimeter.
  3. Minimum Feret's diameter.
  4. Maximum Feret's diameter.

How do I extract color features from an image? ›

To split the color channels into BGR , we can use cv2. split() then use cv2. calcHist() to extract the color features with a histogram.

What is Region selection? ›

Local region selection procedure denotes identification of those local regions having maximum discriminating properties among inter class patterns and minimum discriminating properties among intra class patterns.

How do I save a selected area in ImageJ? ›

A selection can be transferred from one image window to another by activating the destination window and using Edit>Selection>Restore Selection. Selections can be saved to disk using File>Save As>Selection and restored using File>Open.

How do we find items of a specific color in OpenCV? ›

Steps to Detect Objects of Similar Color using OpenCV
  1. Import necessary packages and read the image.
  2. Detect the color from the input image and create a mask.
  3. Removing unnecessary noise from masks.
  4. Apply the mask to the image.
  5. Draw a Boundary of the detected objects.

What is region interest algorithm? ›

Our hierarchical region-of-interest detection algorithm uses five steps: (1) a priori information processing; (2) image downsampling; (3) region-of-candidates (ROCs) detection for each prototype group; (4) ROC arbitration; and (5) ROC area extension to form regions of interest (ROIs).

What is ROI in OpenCV? ›

Many common image operations are performed using Region of Interest in OpenCV. A ROI allows us to operate on a rectangular subset of the image. The typical series of steps to use ROI is: create a ROI on the image, perform the operation you want on this subregion of the image, reset back the ROI.

Which algorithm is used for extraction texture of an image? ›

Polarization Image Texture Feature Extraction Algorithm Based on CS-LBP Operator.

How do you feature extraction? ›

Feature extraction refers to the process of transforming raw data into numerical features that can be processed while preserving the information in the original data set. It yields better results than applying machine learning directly to the raw data.

What is the process of removing unwanted part of the selected picture called? ›

The correct answer is Cropping. It is the removal of unwanted outer areas from a photographic or illustrated image.

What three tools can be used for selecting a portion of an image? ›

The marching ants are in the same place.
  • Marquee Tools. The Marquee tools let you drag a shape over an area to select it. ...
  • Lasso. The Lasso tool lets you draw freely around the object you want to select. ...
  • Polygonal Lasso. ...
  • Magnetic Lasso. ...
  • Object Selection. ...
  • Quick Selection. ...
  • Magic Wand. ...
  • Select Color Range.

Videos

1. Python Image Segmentation Tutorial (2022)
(Mr. P Solver)
2. Python tips and tricks - 5: Extracting patches from large images and masks for semantic segmentation
(DigitalSreeni)
3. Practical OpenCV 3 Image Processing with Python : Extracting Contours from Images | packtpub.com
(Packt )
4. Python OpenCV Grabcut Image Foreground Detection
(Parwiz Forogh)
5. Step to step segmentation of selected area in image using OPENCV C++
(Robotics Workshop)
6. Extracting Images and Masks from Video Segmentation Dataset using OpenCV and Python
(Idiot Developer)
Top Articles
Latest Posts
Article information

Author: Manual Maggio

Last Updated: 04/09/2023

Views: 6008

Rating: 4.9 / 5 (69 voted)

Reviews: 92% of readers found this page helpful

Author information

Name: Manual Maggio

Birthday: 1998-01-20

Address: 359 Kelvin Stream, Lake Eldonview, MT 33517-1242

Phone: +577037762465

Job: Product Hospitality Supervisor

Hobby: Gardening, Web surfing, Video gaming, Amateur radio, Flag Football, Reading, Table tennis

Introduction: My name is Manual Maggio, I am a thankful, tender, adventurous, delightful, fantastic, proud, graceful person who loves writing and wants to share my knowledge and understanding with you.