I need to find the area of an irregular object, for example, area of this lemon sketch. Here was my algorithm
- Put a coin nearby
- measure its radius in pixels,
- knowing its real radius calculate pixel to mm ratio.
- somehow remove bg from the sketch
- calculate its area in pixels (just by counting them)
- multiply by the known ratio to find its actual area.
And I fount some problems:
- The cv2 houghcircles method didn't work when there were some other objects nearby
- 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)
Thank you!