OpenCV 图像处理管道从植物图像中分割花朵

问题描述 投票:0回答:1

我被要求使用 OpenCV python 开发一个图像处理管道,以从植物图像中分割花朵。但我仍然无法获得准确的二值图像。

这是我当前的管道代码:

def process_image(image_path):
    # Read the image
    image = cv2.imread(image_path)

    # Apply bilateral filter for noise reduction
    noise_reduced_image = cv2.bilateralFilter(image, d=9, sigmaColor=75, sigmaSpace=75)

    # Convert to grayscale
    grayscale_image = cv2.cvtColor(noise_reduced_image, cv2.COLOR_BGR2GRAY)

    # Apply Gaussian blur to reduce noise
    blurred_image = cv2.GaussianBlur(grayscale_image, (5, 5), 0)

    # Threshold the image - this value may need adjustment for your images
    ret, thresholded_image = cv2.threshold(blurred_image, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)

    # Find contours from the binary image
    contours, hierarchy = cv2.findContours(thresholded_image, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

    # Create an empty image for contours which is the same size as the original image
    contour_image = np.zeros_like(thresholded_image)

    # Draw the contours on the contour image
    cv2.drawContours(contour_image, contours, -1, (255), thickness=cv2.FILLED)

    # Perform morphological operations to further clean up the image
    kernel = np.ones((5, 5), np.uint8)
    contour_image = cv2.morphologyEx(contour_image, cv2.MORPH_OPEN, kernel, iterations=7)  # Remove noise
    dilated_image = cv2.dilate(contour_image, kernel, iterations=1)  # Fill in the gaps

    final_image = cv2.bitwise_not(dilated_image)
    return final_image

输入图片:

基本事实:

我得到了什么:

opencv image-processing image-segmentation noise-reduction image-thresholding
1个回答
0
投票

我尝试过滤图像的黄色部分。

import cv2
import numpy as np

def process_image(image_path):
    image = cv2.imread(image_path)
    image = cv2.GaussianBlur(image, (11, 11), 0) #Noise removal
    image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV) #Convert to HSV
    lower = np.array([22, 93, 0], dtype="uint8") #Lower value for yellow
    upper = np.array([45, 255, 255], dtype="uint8") #Upper value for yellow
    mask = cv2.inRange(image, lower, upper) #filter only areas in YELLOW colour.
    mask = cv2.erode(mask, None, iterations=2) #Erode to remove noise
    mask = cv2.dilate(mask, None, iterations=2) #dilate to close gaps
    contours, hierarchy = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) #get contour

    selected_contours = []
    for ct in contours:
        if cv2.contourArea(ct) > 10000: #Select contours greater than a particular area value.
            selected_contours.append(ct)
    contour_image = np.zeros_like(mask)
    
    cv2.drawContours(contour_image, selected_contours, -1, (255), thickness=cv2.FILLED) # draw contour of selected contours
    return contour_image


result = process_image("flower.jpg")
cv2.imwrite("result.jpg",result)

输出:

© www.soinside.com 2019 - 2024. All rights reserved.