-1

I am new to OpenCV. I have two questions to ask.

  1. I am trying to print the no of contours available after applying the area. I am getting the correct output in imshow but not in the print statement. I understood that print(len(contours)) gives the total number of contours but I need the no of contours in the given area > 400. You can check the below python code for more details. Please help me with this.
  2. Is it possible to change the threshold value above 255, whenever I change it to above 255, I am getting the black image even if I increase the max value?

Thank you!

import cv2 as cv
import numpy as np

im_color = cv.imread("D:\python_project\Focus_detection_1/_00005_cells.png", cv.IMREAD_COLOR)
im_gray = cv.cvtColor(im_color, cv.COLOR_BGR2GRAY)

_, thresh = cv.threshold(im_gray, thresh=254, maxval=255, type=cv.THRESH_BINARY)  
mask = cv.cvtColor(thresh, cv.COLOR_GRAY2BGR)

im_thresh_color = cv.bitwise_and(im_color,mask) 
contours, hierarchy = cv.findContours(thresh, cv.RETR_TREE,cv.CHAIN_APPROX_SIMPLE)
cv.drawContours(im_thresh_color,contours,-1,(0,0,255),2)

for c in contours:
    area = cv.contourArea(c)

    if area > 400:

        x,y,w,h = cv.boundingRect(c)
        im = cv.rectangle(im_thresh_color, (x,y), (x+w, y+h), (255,0,0), 2)
        cv.drawContours(im_thresh_color,contours, -1,(0,0,255),2) #-1 is to draw all the contour, 0 is the 1st contour and so on

        text = cv.putText(im, 'Focused', (x, y), cv.FONT_HERSHEY_SIMPLEX, 0.5, (36, 255, 12), 2)
        no_of_images = len(contours)
        print("images:", no_of_images)

while True:
    cv.imshow("original image", im_color)
    cv.imshow("Thresh color with contour", im_thresh_color)
    #print("n:",len(im_thresh_color))

    if cv.waitKey(1) == ord("n"):
        break

cv.destroyAllWindows()
Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36
Sharmila
  • 1
  • 1

1 Answers1

0

Here is the updated code for your first question: This code counts how many contours are in the area and then prints the number of contours in that area

import cv2 as cv
import numpy as np

im_color = cv.imread("D:\python_project\Focus_detection_1/_00005_cells.png", cv.IMREAD_COLOR)
im_gray = cv.cvtColor(im_color, cv.COLOR_BGR2GRAY)

_, thresh = cv.threshold(im_gray, thresh=254, maxval=255, type=cv.THRESH_BINARY)
mask = cv.cvtColor(thresh, cv.COLOR_GRAY2BGR)

im_thresh_color = cv.bitwise_and(im_color, mask)
contours, hierarchy = cv.findContours(thresh, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)
cv.drawContours(im_thresh_color, contours, -1, (0, 0, 255), 2)

counter = 0

for c in contours:

    area = cv.contourArea(c)

    if area > 400:

        x, y, w, h = cv.boundingRect(c)
        im = cv.rectangle(im_thresh_color, (x, y), (x+w, y+h), (255, 0, 0), 2)
        # -1 is to draw all the contour, 0 is the 1st contour and so on
        cv.drawContours(im_thresh_color, contours, -1, (0, 0, 255), 2)

        text = cv.putText(im, 'Focused', (x, y), cv.FONT_HERSHEY_SIMPLEX, 0.5, (36, 255, 12), 2)
        counter += 1

print("images:", counter)

while True:
    cv.imshow("original image", im_color)
    cv.imshow("Thresh color with contour", im_thresh_color)
    # print("n:",len(im_thresh_color))

    if cv.waitKey(1) == ord("n"):
        break

cv.destroyAllWindows()

Answer question 2: no it is not possible to change it over a value of 255 bc the range of the r,g,b values is from 0 to 255.