灰度图像中OpenCV轮廓图的平均强度

时间:2020-08-17 18:19:31

标签: python image numpy opencv

Artificial image for testing

有了这个人造图像,我能够找到它们并创建一个蒙版,然后使用两个丑陋的for循环计算它们的平均强度(灰度),这会花费大量时间,并且图像中包含相当数量的数字。< / p>

#!/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)

正如在一条评论中已经建议的那样,我可以使用NumPy来计算其平均强度,但是我不能仅用图形的像素来计算它,而是将其余像素相加。

是否可以使用其他任何更快的方法来实现?

谢谢。

1 个答案:

答案 0 :(得分:1)

最后,这是一个可行的解决方案,再次感谢@ fmw42向我指出正确的方向,并感谢@imochoa提供了最短的工作代码。

#!/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)