calculating the area dimension in image

asked 2020-11-11 14:27:47 -0500

kilwa gravatar image

updated 2020-11-20 02:14:39 -0500

is there a why to measure different areas of black object in image ? the goal is to detect if tube has a bobbin or not. here's an image example

i have tried to use binary image that contains a known number of blobs that vary in shape and size, it doesn't seems to get good results. here's my code

#!/usr/bin/env python3

import numpy as np
import cv2

# Read input image
img = cv2.imread('img22.jpg')

# Convert from BGR to HSV color space
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

# Get the saturation plane - all black/white/gray pixels are zero, and colored pixels are above zero.
s = hsv[:, :, 1]

# Apply threshold on s - use automatic threshold algorithm (use THRESH_OTSU).
ret, thresh = cv2.threshold(s, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)

# Find contours in thresh (find only the outer contour - only the rectangle).
contours = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)[-2]  # [-2] indexing takes return value before last (due to OpenCV compatibility issues).

# Mark rectangle with green line
cv2.drawContours(img, contours, -1, (0, 255, 0), 2)

# Assume there is only one contour, get the bounding rectangle of the contour.
x, y, w, h = cv2.boundingRect(contours[0])

# Invert polarity of the pixels inside the rectangle (on thresh image).
thresh[y:y+h, x:x+w] = 255 - thresh[y:y+h, x:x+w]

# Find contours in thresh (find the triangles).
contours = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)[-2]  # [-2] indexing takes return value before last (due to OpenCV compatibility issues).

blank = np.zeros(img.shape, dtype=np.uint8)

# Iterate triangle contours
for c in contours:
    area = cv2.contourArea(c)
    print("area ",area)
    hull = cv2.convexHull(c)
    cv2.drawContours(img, [hull], -1, (0, 0, 255), 1) 

        # Mark triangle with blue line
        #cv2.drawContours(img, [c], -1, (255, 0, 0), 2)

# Show result (for testing).
cv2.imshow('img', img)
cv2.imshow('thresh', thresh)

any suggestions or guide lines please !? thanks in advance.

edit retag flag offensive close merge delete


any help ?

kilwa gravatar imagekilwa ( 2020-11-12 09:37:40 -0500 )edit

explain what you mean by "measure". in general, explain what the problem is, and what the goal is, and what you did. post code and images.

crackwitz gravatar imagecrackwitz ( 2020-11-15 09:04:14 -0500 )edit

i have edited my question. i it clear enough now ?

kilwa gravatar imagekilwa ( 2020-11-15 11:20:50 -0500 )edit

okay, that thing on the cable you describe is a ferrite bead do you only need to evaluate its presence/absence on a fixed close crop, or do you need to find where such a bead is on a picture of a piece of cable?

crackwitz gravatar imagecrackwitz ( 2020-11-15 11:44:53 -0500 )edit

sorry i didn't knew it really name. thanks for correction. actually i need to evaluate the presence/absence of the ferrite bead on the cable. what should i do Please? thanks in advance.

kilwa gravatar imagekilwa ( 2020-11-15 12:28:46 -0500 )edit

please post a camera picture from the situation you actually have to deal with. I'm trying to determine how many unknowns you have to deal with. you posted two close crops of a bead. if you had those pictures already, evaluation would be easy. if you do NOT know where to look for the bead, that adds complexity. if the cable is just thrown on a table, that is a lot harder than handling straight cable.

crackwitz gravatar imagecrackwitz ( 2020-11-15 16:45:09 -0500 )edit

is it clear enough now ? i hope it helps !

kilwa gravatar imagekilwa ( 2020-11-16 01:50:04 -0500 )edit

your task seems to be "automated optical inspection". a cable arranged any which way, on a cluttered work desk, in arbitrary lighting, is no way to do that. you have two options: (1) fix everything so the image analysis part becomes feasible (2) incur orders of magnitude more difficulty in the analysis part, which would require very powerful but computationally expensive methods such as Deep Learning

crackwitz gravatar imagecrackwitz ( 2020-11-16 07:00:48 -0500 )edit

U wanna to measurng black cable and second image..u wanna to measuring black spike?

supra56 gravatar imagesupra56 ( 2020-11-16 07:45:59 -0500 )edit

yes i do ! any suggestions ?

kilwa gravatar imagekilwa ( 2020-11-16 09:51:04 -0500 )edit