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. 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
# ^Now the mask is a black and white image.
# ^Get height of the black region in this image

# Bitwise-AND mask and original image
cv2.imshow('frame',frame)
cv2.imshow('res',res)``` Sep 21, 2018 in Python 184 views

## 1 answer to this question.

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
thresh_min,thresh_max = 127,255
# 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.
print("Distances: vertical: %d, horizontal: %d" % (h,w))```  answered Sep 21, 2018 by
• 56,520 points

+1 vote

## Measuring the distance between pixels on OpenCv with Python

You can try this: Mat pts1(nPts, 1, CV_8UC2), ...READ MORE

+1 vote

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

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

+1 vote

## What's the difference between eval, exec, and compile in Python?

exec is not an expression: a statement ...READ MORE

## how do i change string to a list?

suppose you have a string with a ...READ MORE

## how can i randomly select items from a list?

if you google it you can find. ...READ MORE

+1 vote

## how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

## how do i use the enumerate function inside a list?

can you give an example using a ...READ MORE