Answered Essay: I have a color detection code using the Pi Camera and OpenCV. I need help with WHILE and ELSE or IF and E

I have a color detection code using the Pi Camera and OpenCV. I need help with WHILE and ELSE or IF and ELSE statements in the code on recognizing a particular color. I want to know how to write the statement that results in a reaction after the code is detected. Part of the code is below;

#thresh = cv2.inRange(hsv,np.array((0, 200, 200)), np.array((20, 255, 255)))

lower = np.array([4,10,120],dtype=”uint8″)

upper = np.array([90,100,255], dtype=”uint8″)

thresh = cv2.inRange(blur, lower, upper)
thresh2 = thresh.copy()

# find contours in the threshold image
image, contours,hierarchy = cv2.findContours(thresh,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)

# finding contour with maximum area and store it as best_cnt
max_area = 0
best_cnt = 1
for cnt in contours:
area = cv2.contourArea(cnt)
if area > max_area:
max_area = area
best_cnt = cnt

# finding centroids of best_cnt and draw a circle there
M = cv2.moments(best_cnt)
cx,cy = int(M[‘m10’]/M[‘m00’]), int(M[‘m01’]/M[‘m00’])
#if best_cnt>1:,(cx,cy),10,(100,100,255),-1)
cv2.putText(blur, “Red Detected”, (cx – 20, cy – 20),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2)
# show the frame
cv2.imshow(“Frame”, blur)
key = cv2.waitKey(1) & 0xFF

# clear the stream in preparation for the next frame

*while (thresh = cv2.inRange(blur, lower, upper)):

# if the `q` key was pressed, break from the loop
if key == ord(“q”):

Expert Answer


Option 1:

You can get the bounds for the colours you need:

upper = np.array([210,90,70], dtype=”uint8″)

lower = np.array([76,31,4],dtype=”uint8″)

red_upper =np.array([0,0,255],dtype=”uint8″)

red_lower = np.array([30,30,200],dtype=”uint8″)

green_upper =np.array([0,255,0],dtype=”uint8″)

green_lower = np.array([30,200,30],dtype=”uint8″)

blue_upper =np.array([2550,0,0],dtype=”uint8″)

blue_lower = np.array([200,30,30],dtype=”uint8″)

Now you can detect each color using a function.

Option 2:

Alternatively, you can use Python to define your colours and then detect them:

colourLimits =


([17, 15, 100], [50, 56, 200]),

([86, 31, 4], [220, 88, 50]),

([25, 146, 190], [62, 174, 250]),

([103, 86, 65], [145, 133, 128])


Now you can employ colour detection –

# loop over the colour limits

for (lower, upper) in the limits:

# create NumPy arrays from the colour limits

upper = np.array(upper, dtype = “uint8”)

lower = np.array(lower, dtype = “uint8”)

# now apply a mask for the colours

mask = cv2.inRange(image, lower, upper)

output = cv2.bitwise_and(image, image, mask = mask)

# now have the images shown

cv2.imshow(“images”, np.hstack([image, output]))


Buy Essay
Calculate your paper price
Pages (550 words)
Approximate price: -

Help Me Write My Essay - Reasons:

Best Online Essay Writing Service

We strive to give our customers the best online essay writing experience. We Make sure essays are submitted on time and all the instructions are followed.

Our Writers are Experienced and Professional

Our essay writing service is founded on professional writers who are on stand by to help you any time.

Free Revision Fo all Essays

Sometimes you may require our writers to add on a point to make your essay as customised as possible, we will give you unlimited times to do this. And we will do it for free.

Timely Essay(s)

We understand the frustrations that comes with late essays and our writers are extra careful to not violate this term. Our support team is always engauging our writers to help you have your essay ahead of time.

Customised Essays &100% Confidential

Our Online writing Service has zero torelance for plagiarised papers. We have plagiarism checking tool that generate plagiarism reports just to make sure you are satisfied.

24/7 Customer Support

Our agents are ready to help you around the clock. Please feel free to reach out and enquire about anything.

Try it now!

Calculate the price of your order

Total price:

How it works?

Follow these simple steps to get your paper done

Place your order

Fill in the order form and provide all details of your assignment.

Proceed with the payment

Choose the payment system that suits you most.

Receive the final file

Once your paper is ready, we will email it to you.


Let us write that nagging essay.


Submit Your Essay/Homework Instructions

By clicking on the "PLACE ORDER" button, tell us your requires. Be precise for an accurate customised essay. You may also upload any reading materials where applicable.


Pick A & Writer

Our ordering form will provide you with a list of writers and their feedbacks. At step 2, its time select a writer. Our online agents are on stand by to help you just in case.


Editing (OUR PART)

At this stage, our editor will go through your essay and make sure your writer did meet all the instructions.


Receive your Paper

After Editing, your paper will be sent to you via email.

× How can I help you?