Iris G.
Iris G.

Reputation: 463

OpenCV average intensity of contour figures in grayscale image

Artificial image for testing

Having this artificial image I am able to find the figures and create a mask of them then calculate their average intensity (grayscale) with two ugly for loops which consume a lot of time with a decent number of figures in the image.

#!/usr/bin/env python3
import imutils
import cv2
import numpy as np
from scipy.stats import norm

image = cv2.imread("./test_images/test_artificial2.png")
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Noise reduction
blurred = cv2.GaussianBlur(gray, (3, 3), 0)

# thresholding
mean, std=norm.fit(blurred)
thresh_min_value = int(mean + 3.6*std)
thresh = cv2.threshold(blurred, thresh_min_value, 255, cv2.THRESH_BINARY)[1]

# find contours in the thresholded image
cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,
        cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)

for c in cnts:
    # Moments
    M = cv2.moments(c)
    # Area of contour
    area = M["m00"]

    # Centroid of contour
    cX = int(M["m10"] / M["m00"])
    cY = int(M["m01"] / M["m00"])

    # Perimeter
    perimeter = cv2.arcLength(c, True)

    print(f'{area=}')
    print(f'{perimeter=}')

    epsilon = 0.02 * perimeter
    approx = cv2.approxPolyDP(c, epsilon, True)
    print(f'{approx=}')

    # Create blank mask
    mask_contour = np.zeros(gray.shape, np.uint8)

    # Draw contour in the mask
    cv2.drawContours(mask_contour, [approx], -1, (255, 255, 255), -1)

    # This calculates the intensity of the polygon correctly
    intensity = []
    for i in range(0, gray.shape[0]):
        for j in range(0, gray.shape[1]):
            if mask_contour[i][j] == 255:
                intensity.append(gray[i][j])

    print(sum(intensity)/len(intensity))

    # But I would like to speed up the process somehow
    #masked_image = np.where(mask_contour == 255, gray, 0)
    #average_intensity = np.mean(masked_image)
    # print(f'{average_intensity=}')

    cv2.imshow("Image", mask_contour)
    cv2.waitKey(0)

As already suggested in one comment, I could use NumPy to calculate its mean intensity, but I can't get it to calculate it just with the figures' pixels, it adds the rest of the pixels.

Is it possible to achieve this with any other faster method?

Thanks.

Upvotes: 2

Views: 3589

Answers (1)

Iris G.
Iris G.

Reputation: 463

So, in the end, this is a working solution, thanks again to @fmw42 for pointing me in the right direction and to @imochoa for giving the shortest working code.

#!/usr/bin/env python3
import imutils
import cv2
import numpy as np
from scipy.stats import norm

image = cv2.imread("./test_images/test_artificial2.png")
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Noise reduction
blurred = cv2.GaussianBlur(gray, (3, 3), 0)

# thresholding
mean, std = norm.fit(blurred)
thresh_min_value = int(mean + 3.6*std)
thresh = cv2.threshold(blurred, thresh_min_value, 255, cv2.THRESH_BINARY)[1]

# find contours in the thresholded image
cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,
        cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)

for c in cnts:
    # Moments
    M = cv2.moments(c)
    # Area of contour
    area = M["m00"]

    # Centroid of contour
    cX = int(M["m10"] / M["m00"])
    cY = int(M["m01"] / M["m00"])

    # Perimeter
    perimeter = cv2.arcLength(c, True)

    print(f'{area=}')
    print(f'{perimeter=}')

    epsilon = 0.02 * perimeter
    approx = cv2.approxPolyDP(c, epsilon, True)
    print(f'{approx=}')

    # Create blank image
    blank_image = np.zeros(gray.shape, np.uint8)

    # Draw contour in the mask
    cv2.drawContours(blank_image, [approx], -1, (255, 255, 255), -1)

    # Create a mask to select pixels inside the figure
    mask_contour = blank_image == 255

    # Calculate the intensity from the grayscale image
    # filtering out the pixels where in the blank_image their value is not 255
    intensity = np.mean(gray[mask_contour])
    print(f'{intensity=}')

    cv2.imshow("Image", blank_image)
    cv2.waitKey(0)

Upvotes: 3

Related Questions