How to detect width of object in picture

568 Views Asked by At

I must write the program to detect width of object. I understand that without reference object it will be expressed in pixels but it's enough for me. The background will always be white. I have problem what i should to do right now.

I will be sow greatfull for Your help ! enter image description here

import numpy as np
import imutils
import cv2
import math

# Function to show array of images (intermediate results)
def show_images(images):
    for i, img in enumerate(images):
        cv2.imshow("image_" + str(i), img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()


# Read image and preprocess
image = cv2.imread('44.jpg')

gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (9, 9), 0)

edged = cv2.Canny(blur, 50, 100)
edged = cv2.dilate(edged, None, iterations=1)
edged = cv2.erode(edged, None, iterations=1)

show_images([blur, edged])

#show_images([cnts, edged])
1

There are 1 best solutions below

0
Ikechukwu Anude On

Welcome to Stack Overflow!

Since your're using OpenCV, finding the image dimensions is as simple as the code below.

import numpy as np
import cv2

img = cv2.imread('image.png')

dimension = img.shape


height = img.shape[0]
width = img.shape[1]
channels = img.shape[2] 

Read more about this here: