+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 10,291 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
• 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
• 69,230 points

0 votes
1 answer

## Measuring the distance between pixels on OpenCv with Python

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

0 votes
1 answer

## Measuring the distance between pixels on OpenCv with Python

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

0 votes
0 answers

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

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

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

0 votes
1 answer

## What is the difference between Python and IPython?

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

+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

+2 votes
2 answers

## How to make a laplacian pyramid using OpenCV python?

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

+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