Measuring the distance between pixels on OpenCv with Python

+1 vote
I'm a newbie with Open CV and computer vision so I humbly ask a question.
With a pi camera I record a video and in real time I can recognize blue from other colors (I see blue as white and other colors as black).
I want to measure the length of the base of the area (because I have a white rectangle and a black rectangle). This two rectangle together create the square frame.

I'm a newbie with Open CV and computer vision so I humbly ask a question. With a pi camera I record a video and in real time I can recognize blue from other colors (I see blue as white and other colors as black).

enter image description here

I want to measure the length of the base of the area (because I have a white rectangle and a black rectangle). This two rectangle together create the square frame.

enter image description here

Excerpt of code:

# Take each frame
_, frame = cap.read(0)

# Convert BGR to HSV
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)

# define range of blue color in HSV
lower_blue = np.array([80,30,30])
upper_blue = np.array([130,150,210])

# Threshold the HSV image to get only blue colors
mask = cv2.inRange(hsv, lower_blue, upper_blue)    
# applica filtri morfologici
mask = cv2.erode(mask, spotFilter)
mask = cv2.dilate(mask, maskMorph)
# ^Now the mask is a black and white image.
# ^Get height of the black region in this image

# Bitwise-AND mask and original image
res = cv2.bitwise_and(frame,frame, mask= mask)
cv2.imshow('frame',frame)
cv2.imshow('mask',mask)
cv2.imshow('res',res)
May 9, 2018 in Python by aryya
• 7,460 points
10,555 views

2 answers to this question.

0 votes
Assuming input frames will have "close to rectangle" shapes (where the following code works best), you have to use the findContours function to get the black region's boundary and boundingRectfunction to get it's dimensions.

mask = cv2.imread('mask.png') #The mask variable in your code
# plt.imshow(mask)
thresh_min,thresh_max = 127,255
ret,thresh = cv2.threshold(mask,thresh_min,thresh_max,0)
# findContours requires a monochrome image.
thresh_bw = cv2.cvtColor(thresh, cv2.COLOR_BGR2GRAY)
# findContours will find contours on bright areas (i.e. white areas), hence we'll need to invert the image first
thresh_bw_inv = cv2.bitwise_not(thresh_bw)

_, contours, hierarchy = cv2.findContours(thresh_bw_inv,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
# ^Gets all white contours

# Find the index of the largest contour
areas = [cv2.contourArea(c) for c in contours]
max_index = np.argmax(areas)
cnt=contours[max_index]

x,y,w,h = cv2.boundingRect(cnt)
#Draw the rectangle on original image here.
cv2.rectangle(mask,(x,y),(x+w,y+h),(0,255,0),2)
plt.imshow(mask)
print("Distances: vertical: %d, horizontal: %d" % (h,w)
answered May 9, 2018 by charlie_brown
• 7,720 points
0 votes

You can try this:

Mat pts1(nPts, 1, CV_8UC2), pts2(nPts, 1, CV_8UC2);
// populate them
Mat diffPts = pts1-pts2;
Mat ptsx, ptsy;
// split your points in x and y vectors. maybe separate them from start
Mat dist;
magnitude(ptsx, ptsy, dist); // You have found the distance.
answered Aug 24, 2018 by Omkar
• 69,220 points

Related Questions In Python

0 votes
1 answer

Measuring the distance between pixels on OpenCv with Python

Assuming input frames will have "close to ...READ MORE

answered Sep 11, 2018 in Python by Priyaj
• 58,020 points
5,706 views
0 votes
1 answer

Measuring the distance between pixels on OpenCv with Python

Assuming input frames will have "close to ...READ MORE

answered Sep 21, 2018 in Python by Priyaj
• 58,020 points
2,732 views
0 votes
0 answers

I am trying to install PyBase64 on my python 3.8. But I end up with the following error:

$ pip install pybase ERROR: Could not find ...READ MORE

Mar 24, 2020 in Python by Nishant
• 210 points
3,005 views
0 votes
1 answer

I am trying to install os-win on my python 3.8. But I end up with the following error:

I have the same issue and is ...READ MORE

answered Sep 6, 2020 in Python by anonymous
7,924 views
0 votes
1 answer

I am trying to install visualize on my python 3.8 but i end up with the followinf error.

Hi@Avinash, I think the module name is visualization ...READ MORE

answered Apr 27, 2020 in Python by MD
• 95,460 points
1,588 views
0 votes
1 answer

What is the difference between Python and IPython?

There are few differences between Python and ...READ MORE

answered Jul 26, 2018 in Python by Priyaj
• 58,020 points
4,008 views
+1 vote
1 answer

What is the difference between range and xrange functions in Python 2.X?

xrange only stores the range params and ...READ MORE

answered Aug 22, 2018 in Python by Priyaj
• 58,020 points
2,400 views
+2 votes
2 answers

How to make a laplacian pyramid using OpenCV python?

down voteacceptTheeThe problem is that you're iterating ...READ MORE

answered Apr 3, 2018 in Python by charlie_brown
• 7,720 points
4,913 views
+2 votes
3 answers

How can I play an audio file in the background using Python?

down voteacceptedFor windows: you could use  winsound.SND_ASYNC to play them ...READ MORE

answered Apr 4, 2018 in Python by charlie_brown
• 7,720 points
13,552 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP