计算RGB图像中的百分比降低 [英] Calculate Percentage Decrease in RGB Image

查看:40
本文介绍了计算RGB图像中的百分比降低的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一种图像处理算法,该算法涉及在图像上放置蒙版并按如下所示隐藏某些区域,

I'm working on an Image Processing Algorithm that involves placing a mask on an image and hiding certain regions as follows,

图片1

图片1缩小

从视觉上看,图像1减少了约50%.

Visually Image 1 has undergone a reduction of about 50%.

我处理过的某些图像最终也具有孤立的区域,如下面的图像.

Some of the images I work with also end up having isolated regions like the one below.

要计算图像的缩小量,我使用以下代码计算了黑色像素的增加量,

To compute reduction in the image I used the following code to calculate the increase in black pixels,

sought = [0,0,0]
black1  = np.count_nonzero(np.all(Image1==sought,axis=2)) #Black pixels in Image 1
black2  = np.count_nonzero(np.all(Image1_reduced==sought,axis=2)) #Black pixels in Image 1 Reduced

reduction = ((black2-black1)/black1)*100

print("Reduction %: ", round(reduction, 2))

但是,代码块显示了错误的减少值.有什么替代方法吗?

However the code block shows erroneous values for reduction. Are there any alternatives to this approach?

推荐答案

您必须将总像素数而不是黑色像素数放在分母中.

You have to put the total number of pixels in the denominator instead of the number of black pixels.

In [161]: import numpy as np

In [162]: sought = [0, 0, 0]

In [163]: rows = cols = 256

In [164]: original = np.random.randint(
     ...:     low=0, high=256, size=(rows, cols, 3), dtype=np.uint8)
     ...: original[:rows//4, :, :] = sought

In [165]: masked = original.copy()
     ...: masked[:, :cols//4, :] = sought

In [166]: black1 = np.count_nonzero((original == sought).all(axis=-1))

In [167]: black2 = np.count_nonzero((masked == sought).all(axis=-1))

In [168]: import matplotlib.pyplot as plt
     ...: fig, (ax0, ax1) = plt.subplots(1, 2)
     ...: ax0.imshow(original)
     ...: ax0.set_title('Original')
     ...: ax1.imshow(masked)
     ...: ax1.set_title('Masked')
     ...: plt.show(fig)

In [169]: reduction = 100*(black2 - black1)/(rows*cols)

In [170]: print(f'Reduction = {reduction:.2f}%')
Reduction = 18.75%

在上面的示例中,黑色像素的增加是1/4-1/16,等于0,1875.

In the example above the increase of black pixels is 1/4 - 1/16 which equals 0,1875.

也许图像在视觉上似乎具有约50%的减小",但实际上减小为36.61%.运行此代码以说服自己.

Maybe that "the images visually seem to have about 50% reduction", but the reduction is actually 36.61%. Run this code to convince yourself.

In [192]: from skimage import io

In [193]: original = io.imread('https://i.stack.imgur.com/PwVmP.png')

In [194]: masked = io.imread('https://i.stack.imgur.com/OG2aF.png')

In [195]: non_black_pixels_1 = np.logical_not((original == sought).all(axis=-1))

In [196]: black_pixels_2 = (masked == sought).all(axis=-1)

In [197]: new_black_pixels = np.logical_and(black_pixels_2, non_black_pixels_1)

In [198]: import matplotlib.pyplot as plt
     ...: fig, (ax1, ax2, ax3) = plt.subplots(1, 3)
     ...: ax1.imshow(original)
     ...: ax1.set_title('Image 1')
     ...: ax2.imshow(masked)
     ...: ax2.set_title('Image 1 Reduced')
     ...: ax3.imshow(new_black_pixels)
     ...: ax3.set_title('New black pixels')
     ...: plt.show(fig)

In [199]: reduction = 100*new_black_pixels.mean()

In [200]: print(f'Reduction = {reduction:.2f}%')
Reduction = 36.61%


编辑#2

根据评论,您似乎正在尝试计算非黑色像素在蒙版后变黑的百分比.以下代码片段正是这样做的:


Edit #2

As per the comments, it seems that you are trying to compute the percentage of non-black pixels that become black after masking. The following snippet does exactly that:

In [219]: nonblack1 = np.count_nonzero((original != sought).any(axis=-1))

In [220]: nonblack2 = np.count_nonzero((masked != sought).any(axis=-1))

In [221]: reduction = 100*(nonblack1 - nonblack2)/nonblack1

In [222]: print(f'Reduction = {reduction:.2f}%')
Reduction = 55.85%

这篇关于计算RGB图像中的百分比降低的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆