Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

calculating the area dimension in image

is there a why to measure different areas of black object in image ? here's an image example image description 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. any suggestions or guide lines please !? thanks in advance.

calculating the area dimension in image

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 image description exampleimage description(/upfiles/1605460381736981.jpg) 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('img2.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)
    #pass

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

cv2.imshow('thresh', thresh) cv2.waitKey(0) cv2.destroyAllWindows()

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

calculating the area dimension in image

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 exampleimage description(/upfiles/1605460381736981.jpg) 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

#!/usr/bin/env python3


import numpy as np np
import cv2

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

cv2.imread('img_test/ok_bobino2.jpg') # Convert from BGR to HSV color space space hsv = cv2.cvtColor(img,

cv2.COLOR_BGR2HSV)

cv2.cvtColor(img, cv2.COLOR_BGR2HSV) # Get the saturation plane - all black/white/gray pixels are zero, and

and colored pixels are above zero. zero. s = = hsv[:, :, 1]

1] # Apply threshold on s - use automatic threshold algorithm (use THRESH_OTSU).

THRESH_OTSU). ret, thresh = cv2.threshold(s, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)

255, cv2.THRESH_BINARY+cv2.THRESH_OTSU) # Find contours in thresh (find only the outer contour - only the

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

compatibility issues). # Mark rectangle with green line line cv2.drawContours(img, contours, -1,

-1, (0, 255, 0), 2)

2) # Assume there is only one contour, get the bounding rectangle of the

contour. the contour. x, y, w, h = cv2.boundingRect(contours[0])

= cv2.boundingRect(contours[0]) # Invert polarity of the pixels inside the rectangle (on thresh image).

image). thresh[y:y+h, x:x+w] = 255 - - thresh[y:y+h, x:x+w]

x:x+w] # Find contours in thresh (find the triangles). triangles). contours =

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

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

np.zeros(img.shape, dtype=np.uint8) # Iterate triangle contours contours for c in contours:

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)
     #pass

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

img) cv2.imshow('thresh', thresh) cv2.waitKey(0) cv2.destroyAllWindows()

cv2.waitKey(0) cv2.destroyAllWindows()

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

calculating the area dimension in image

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 exampleexample image description image description(/upfiles/1605460381736981.jpg)

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('img_test/ok_bobino2.jpg')
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)
        #pass

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

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

calculating the area dimension in image

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 image description image descriptionexample

image description image description

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)
        #pass

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

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

calculating the area dimension in image

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

image description image description

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)
        #pass

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

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