Home > Net >  How can i find area of an object by photo?
How can i find area of an object by photo?

Time:02-21

I need to find the area of an irregular object, for example, area of this lemon sketch. Here was my algorithm

  1. Put a coin nearby
  2. measure its radius in pixels,
  3. knowing its real radius calculate pixel to mm ratio.
  4. somehow remove bg from the sketch
  5. calculate its area in pixels (just by counting them)
  6. multiply by the known ratio to find its actual area.

And I fount some problems:

  1. The cv2 houghcircles method didn't work when there were some other objects nearby
  2. Remove.bg API worked with only fully coloured objects, so it removed the blankspaces between the strokes.

Could you please suggest any other method, or help me with realising this sort of stuff. The example picture and some code which I managed to write will be below.

Hough Circles

import cv2
import numpy as np

img = cv2.imread('thresh.png',0)
img = cv2.medianBlur(img,5)
cimg = cv2.cvtColor(img,cv2.COLOR_GRAY2BGR)

circles = cv2.HoughCircles(img,cv2.HOUGH_GRADIENT,1,20,
                            param1=50,param2=99,minRadius=100,maxRadius=500)

circles = np.uint16(np.around(circles))
for i in circles[0,:]:
    # draw the outer circle
    cv2.circle(cimg,(i[0],i[1]),i[2],(0,255,0),2)
    # draw the center of the circle
    cv2.circle(cimg,(i[0],i[1]),2,(0,0,255),3)
    print(f"Radius: {i[2]}")
cv2.imshow('detected circles',cimg)
cv2.waitKey(0)
cv2.destroyAllWindows()

Remove.bg API

def clean(path):
    import requests
    response = requests.post(
        'https://api.remove.bg/v1.0/removebg',
        files={'image_file': open('imagepath.png', 'rb')},
        data={'size': 'auto'},
        headers={'X-Api-Key': 'my Api key'},
    )
    if response.status_code == requests.codes.ok:
        with open('no-bg.png', 'wb') as out:
            out.write(response.content)
    else:
        print("Error:", response.status_code, response.text)

enter image description here

Thank you!

CodePudding user response:

To isolate the coin: HoughCircles is a good approach, even though depending on perspective the coin may not be a perfect circle. You can also try the watershed algorithm to segment all objects, then detect which object is a coin by the color or by how circular it is (e.g., which object has the most similar distances between its centroid and different edge points).

To isolate the drawing: If the drawing is the only object left besides the coin, mask the coin out and threshold (e.g. with Otsu) to convert the image to binary. You will still have holes in the drawing, which you can eliminate using opening.

  • Related