Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用遮罩对图像的不同部分应用不同的阈值_Python_Opencv_Image Thresholding - Fatal编程技术网

Python 使用遮罩对图像的不同部分应用不同的阈值

Python 使用遮罩对图像的不同部分应用不同的阈值,python,opencv,image-thresholding,Python,Opencv,Image Thresholding,我有一个图像,我想在一个圆形区域内对图像的一部分设置阈值,然后在这个区域外对图像的其余部分设置阈值 不幸的是,我的尝试似乎将图像作为一个整体进行阈值化,而忽略了遮罩。如何才能恰当地实现这一点?请参阅下面的代码尝试 def circular_mask(h, w, centre=None, radius=None): if centre is None: # use the middle of the image centre = [int(w / 2), int(h /

我有一个图像,我想在一个圆形区域内对图像的一部分设置阈值,然后在这个区域外对图像的其余部分设置阈值

不幸的是,我的尝试似乎将图像作为一个整体进行阈值化,而忽略了遮罩。如何才能恰当地实现这一点?请参阅下面的代码尝试

def circular_mask(h, w, centre=None, radius=None):
    if centre is None:  # use the middle of the image
        centre = [int(w / 2), int(h / 2)]
    if radius is None:  # use the smallest distance between the centre and image walls
        radius = min(centre[0], centre[1], w - centre[0], h - centre[1])

    Y, X = np.ogrid[:h, :w]
    dist_from_centre = np.sqrt((X - centre[0]) ** 2 + (Y - centre[1]) ** 2)

    mask = dist_from_centre <= radius
    return mask

img = cv2.imread('image.png', 0) #read image

h,w = img.shape[:2]
mask = circular_mask(h,w, centre=(135,140),radius=75) #create a boolean circle mask
mask_img = img.copy()

inside = np.ma.array(mask_img, mask=~mask)
t1 = inside < 50 #threshold part of image within the circle, ignore rest of image
plt.imshow(inside)
plt.imshow(t1, alpha=.25)
plt.show()

outside = np.ma.array(mask_img, mask=mask)
t2 = outside < 20 #threshold image outside circle region, ignoring image in circle
plt.imshow(outside)
plt.imshow(t2, alpha=.25)
plt.show()

fin = np.logical_or(t1, t2) #combine the results from both thresholds together
plt.imshow(fin)
plt.show()
def圆形遮罩(h、w、中心=无、半径=无): 如果中心为无:#使用图像的中间部分 中心=[int(w/2),int(h/2)] 如果半径为无:#使用中心和图像墙之间的最小距离 半径=最小值(中心[0],中心[1],w-中心[0],h-中心[1]) Y、 X=np.ogrid[:h,:w] 距离中心的距离=np.sqrt((X-中心[0])**2+(Y-中心[1])**2)
蒙版=距离中心我假设您的图像是单层的(例如灰度)。
您可以复制两份图像。将掩码与其中一个相乘(或逻辑AND),并将该掩码与另一个反转。现在,将所需的阈值应用于每个阈值。最后,使用逻辑“或”运算合并两个图像。

当您谈到将树结构应用于图像时,您的意思是什么?你能给出一个你期望输出的图形例子吗?与之相比,看起来你想做一些完全不同的事情。@Bit目前我只是使用布尔逻辑来查找某个值下的像素。例如,“img<50”在像素足够暗的情况下返回True。我想将其扩展到大津二值化阈值,但过程应该类似于上面的示例。我的阈值应该在满足条件时返回True,否则返回False。我必须使用乘法而不是掩码的逻辑运算,并且必须使用and合并两个图像。有了这个,你的方法很有效,谢谢。
img = cv2.imread('image.png', 0)

h,w = img.shape[:2]
mask = circular_mask(h,w, centre=(135,140),radius=75)


inside = img.copy()*mask
t1 = inside < 50#get_threshold(inside, 1)
plt.imshow(inside)
plt.show()

outside =  img.copy()*~mask
t2 = outside < 70
plt.imshow(outside)
plt.show()

plt.imshow(t1)
plt.show()
plt.imshow(t2)
plt.show()

plt.imshow(np.logical_and(t1,t2))
plt.show()