Ask Your Question
0

Pixel size (area) of the object in an image

asked 2018-01-22 11:55:45 -0600

nyx123 gravatar image

Searching through google about how to get the area of the vehicle in an image of 61x61 , I already used contours , background subtraction but still I cant get an exact output. Any idea on how to get the pixel size of the vehicle in the image below? Btw Im new to opencv your expertise will really help

Thanks

https://ibb.co/j4i3ZG

edit retag flag offensive close merge delete

Comments

Post your contours code.

sjhalayka gravatar imagesjhalayka ( 2018-01-22 12:54:11 -0600 )edit

basically im using the basic from this https://docs.opencv.org/3.3.1/d4/d73/... sorry sir but Im not really good at this. Mostly I use all basic codes for trying to detect the image above. I hope theres some kind of process that I should follow

nyx123 gravatar imagenyx123 ( 2018-01-22 13:01:16 -0600 )edit
sjhalayka gravatar imagesjhalayka ( 2018-01-22 13:07:16 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2018-01-22 14:48:41 -0600

sjhalayka gravatar image

Here is a Python code to get the contours and get the contour areas:

import cv2
import numpy

frame = cv2.imread('blobs.png')

if frame is None:
    print('Error loading image')
    exit()

frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
ret, frame = cv2.threshold(frame, 127, 255, cv2.THRESH_BINARY)

frame, contours, hierarchy = cv2.findContours(frame, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

areas = []

for i in range(0, len(contours)):
    areas.append(cv2.contourArea(contours[i]))

mass_centres_x = []
mass_centres_y = []

for i in range(0, len(contours)):
    M = cv2.moments(contours[i], 0)
    mass_centres_x.append(int(M['m10']/M['m00']))
    mass_centres_y.append(int(M['m01']/M['m00']))

print 'Num particles: ', len(contours)

for i in range(0, len(contours)):
    print 'Area', (i + 1), ':', areas[i]

for i in range(0, len(contours)):
    print 'Centre',(i + 1),':', mass_centres_x[i], mass_centres_y[i]    

cv2.imshow("Frame", frame)

cv2.waitKey(0)
edit flag offensive delete link more

Comments

It appears that the M['m00'] contains zero value which I get this kind of error and after using the image below the contours detected are 7 which I think is not right? Do I need to undergo some pre process of the image?

Error : ZeroDivisionError: float division by zero in this line -> mass_centres_x.append(int(M['m10']/M['m00']))

this is the actual image https://ibb.co/j4i3ZG

nyx123 gravatar imagenyx123 ( 2018-01-22 20:26:03 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2018-01-22 11:55:45 -0600

Seen: 1,750 times

Last updated: Jan 22 '18