0

I want to develop an algorithm to recognize through an image, if the object present in said image is a spoon, a fork, or a knife. To acomplish this I am thinking of first comparing the ratio between white and black pixels along a line and if the ratio is the same along the ROI (with a tolerance of say 30%) i can say it's a knife, if not then I move to determine if it's a spoon or a fork. But my problem starts here, I can't control wheter or not the spoon/fork/knife comes correctly oriented or not, if it comes rotated I am afraid my algorithm needs to be able to check along rotated lines as well. I can alredy get the angle of rotation, but I don't really know what to do with it. The code I have so far is:

import cv2
import numpy as np


img = cv2.imread('GenericImages/PBL3/fork.jpg')
NC = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, threshold = cv2.threshold(NC, 50, 255, cv2.THRESH_BINARY)

contours, hierarchy = cv2.findContours(threshold, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
# Draws contours
for c in contours:
    if cv2.contourArea(c) < 1000:
        continue
    rect = cv2.minAreaRect(c)
    box = cv2.boxPoints(rect)
    box = np.int0(box)
    cv2.drawContours(img, [box], 0, (0, 191, 255), 2)

cv2.imshow('ROI', img)
cv2.waitKey(0)
  • 2
    You can try rotating the image and then use your algorithm for a vertical image. https://stackoverflow.com/questions/9041681/opencv-python-rotate-image-by-x-degrees-around-specific-point – Jay Dec 17 '21 at 20:14
  • Do you worked with HuMoments and matchShapes in openCV? For better results, you can use deep learning – Milad Shaker Dec 19 '21 at 19:01
  • No, I didn't, I followed Jay's suggestions and ended up using mostly only the ratio of white pixels to total pixels along each line of the image to distinguish between a fork, a knife or a spoon – Filipe Almeida Dec 19 '21 at 21:25

0 Answers0