在python中查找遮罩和纹理图片之间的交点

2024-05-29 00:19:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我想找到我的面具之间的交点,它是这样的:

enter image description here

和纹理图像:

enter image description here

我想要这样的东西,但没有蓝线(只是想看到更多的区别),也没有边框中的信息: enter image description here

谢谢

编辑:这是我现在的代码:

from PIL import Image
import os
from PIL import Image, ImageFont, ImageDraw
import numpy as np
import cv2
import cv
import matplotlib.pyplot as plt
from scipy.misc import derivative
import copy
import skimage.feature.texture
from PIL import Image



class TextureWavelets:
    def access_to_images(self, directory_segmentation: str, directory_originals: str, n_displays: int,
                         color_plaque: str):

        count = 0
        for image_name in os.listdir(directory_originals):
            if count < n_displays:
                segmented_image = cv2.imread(directory_segmentation + "/" + image_name)
                segmented = Image.open(directory_segmentation + "/" + image_name)
                original = Image.open(directory_originals + "/" + image_name)

                masked = self.create_mask_plaque(segmented_image, color_plaque)
                intersection = self.find_intersection(segmented_image, masked)
                haralick = self.haralick(segmented_image, 'contrast')


    def find_intersection(self, mask, image):
        mask = cv2.cvtColor(mask, cv2.COLOR_BGR2GRAY)
        masked_image = image * mask
        Image.fromarray(masked_image).show()


    def create_mask_plaque(self, image, color_plaque):
        COLOR1_RANGE = [(30, 0, 0), (255, 50, 50)]  # Blue in BGR, [(low), (high)].

        if color_plaque == 'green':
            COLOR1_RANGE = [(0, 30, 0), (50, 255, 50)]
        elif color_plaque == 'red':
            COLOR1_RANGE = [(0, 0, 30), (50, 50, 255)]
        elif color_plaque == 'blue':
            COLOR1_RANGE = [(30, 0, 0), (255, 50, 50)]
        elif color_plaque == 'b&w':
            COLOR1_RANGE = [(0, 0, 255), (255, 255, 255)]

        mask = cv2.inRange(image, COLOR1_RANGE[0], COLOR1_RANGE[1])
        only_plaque = cv2.bitwise_and(image, image, mask=mask)
        gray = cv2.cvtColor(only_plaque, cv2.COLOR_BGR2GRAY)
        blur = cv2.GaussianBlur(gray, (3, 3), 0)
        thresh = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
        thresh = 255 - thresh

        # Morph open with a elliptical shaped kernel
        kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5))
        opening = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel, iterations=2)
        Image.fromarray(opening).show()

        return opening

    def haralick(self, image, function):
        image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
        P = skimage.feature.texture.greycomatrix(image, [1], [0], levels=256, symmetric=False, normed=False)
        result = skimage.feature.texture.greycoprops(P, prop=function)

        return result[0][0]


if __name__ == "__main__":
    texture_wavelets = TextureWavelets()
    directory_segmentation = 'D:/CTU/new_segmentation_data/references/trans'
    directory_originals = 'D:/CTU/new_segmentation_data/data/trans'
    n_displays = 2
    # options for lumen and plaque colors: green, red, blue, b&w
    texture_wavelets.access_to_images(directory_segmentation, directory_originals, n_displays, 'b&w')

问题是find_intersection函数返回以下内容:

enter image description here

它是灰色的,没有纹理,我需要的是纹理


Tags: nameimageimportselfrangemaskcv2directory
1条回答
网友
1楼 · 发布于 2024-05-29 00:19:29

我会这样做:

import cv2
import numpy as np

img = cv2.imread("ring_bg.png")
mask = cv2.imread("ring_mask.png")

h, w, _ = img.shape
mask = cv2.resize(cv2.cvtColor(mask, cv2.COLOR_BGR2GRAY), (w, h)) # Resize image
bg = np.zeros_like(img, 'uint8') # Black background

def crop(img, bg, mask):
    fg = cv2.bitwise_or(img, img, mask=mask)            
    fg_back_inv = cv2.bitwise_or(bg, bg, mask=cv2.bitwise_not(mask))
    return cv2.bitwise_or(fg, fg_back_inv)

cv2.imshow("Image", crop(img, bg, mask))
cv2.waitKey(0)

输出:

enter image description here

相关问题 更多 >

    热门问题