0

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])
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
  • As you said, without a reference, it's impossible to do. – Thomas Schillaci Feb 21 '20 at 08:34
  • Try starting with an Otsu thresholding and then a 9x9 median filter... – Mark Setchell Feb 21 '20 at 09:03
  • It's impossible to get the width in centimeters but, Can i have the positon of most extended object pixel on left and right ? If yes i can calculate the dimension between them and size (still in the piksel but that i said it's enough for me ). Thanks for response! – PatrickM Feb 21 '20 at 09:32
  • 1
    you can use boundingRect, minAreaRect, distanceTransform, medial axis for example. Which one to choose depends on your definition of "width of an object" – Micka Feb 21 '20 at 09:49
  • Thank You for your help , i decide to litte change the code after your suggestion . – PatrickM Feb 21 '20 at 10:23

1 Answers1

0

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:

Ikechukwu Anude
  • 346
  • 1
  • 4
  • 16