简体   繁体   English

如何基于python中的rgb值合并图像中的像素?

[英]How to merge pixels WITHIN an image based on their rgb values in python?

So far , I've divided an image into blocks of specific size and these blocks have the mean color of the original block. 到目前为止,我已经将图像划分为特定大小的块,这些块具有原始块的平均颜色。 Now, I have to merge these blocks based on their similarity, where each block contains a single pixel value(mean color value). 现在,我必须基于它们的相似性来合并这些块,其中每个块都包含一个像素值(平均颜色值)。 For this , I have been trying to merge pixels within an image based on their rgb values. 为此,我一直在尝试根据像素的rgb值合并图像中的像素。 So far I've not found anything that would help me with this. 到目前为止,我还没有发现任何可以帮助我解决这个问题的方法。 So kindly help me to solve this problem. 所以请帮助我解决这个问题。 What I've done so far... 到目前为止我所做的...

x and y are the block sizes. x和y是块大小。 Here x=y=16. 在此,x = y = 16。

Input : Original Image Output: Processed image I've not implemented anything after this since I don't know how to proceed further. 输入: 原始图像输出:经过处理的图像由于我不知道如何进行进一步操作,因此此后我没有执行任何操作。 Now I've to group the pixels in the processed image based on their similarity. 现在,我必须根据它们的相似性对处理后的图像中的像素进行分组。

i=0
j=0
m=16
n=16

l=[]   
data = np.zeros( (256,256,3), dtype=np.uint8 )
while(m<=256):
    while(n<=256):
        l=image[i:m,j:n]

        print(l)
        r=0
        g=0
        b=0
        for q in range(len(l)):
            for w in range(len(l)):
                r=r+l[q][w][0]
                g=g+l[q][w][1]
                b=b+l[q][w][2]

        r=r/(x*y)
        b=b/(x*y)
        g=g/(x*y)
        k=[r,g,b]
        data[i:m,j:n]=k
        j=j+16
        n=n+16

    i=i+16
    m=m+16
    j=0
    n=16
img = smp.toimage( data )
data1 = np.asarray( img, dtype="int32" )


cv2.imwrite(os.path.join('G:/AI package/datasets/_normalized',filename),data1)

You have used quite a lot of code to get the first step done, However, the same output can be achieved using numpy functions within 2-3 lines of code as: 您已经使用了很多代码来完成第一步,但是,在2-3行代码中使用numpy函数可以实现相同的输出,如下所示:

import cv2
import numpy as np


def get_mean_color(box):
    return int(np.mean(box[:, :, 0])), int(np.mean(box[:, :, 1])), int(np.mean(box[:, :, 2]))


def get_super_square_pixels(img, super_pix_width):
    height, width, ch = img.shape

    if height % super_pix_width != 0:
        raise Exception("height must be multiple of super pixel width")

    if width % super_pix_width != 0:
        raise Exception("width must be multiple of super pixel width")

    output_img = np.zeros(img.shape, np.uint8)
    for i in xrange(height / super_pix_width):
        for j in xrange(width / super_pix_width):
            src_box = img[i * super_pix_width:(i + 1) * super_pix_width, j * super_pix_width:(j + 1) * super_pix_width]
            mean_val = get_mean_color(src_box)

            output_img[i * super_pix_width:(i + 1) * super_pix_width, j * super_pix_width:(j + 1) * super_pix_width] = mean_val

    return output_img

img = cv2.imread("/path/to/your/img.jpg")
out = get_super_square_pixels(img, 16)

My code may not be optimal but it works just fine. 我的代码可能不是最佳的,但效果很好。

import cv2
import numpy as np
import scipy.misc as smp
import os


l=[]   
res = np.zeros( (256,256,3), dtype=np.uint8 )
while(m<=256):
    while(n<=256):
        l=image[i:m,j:n][0][0]
        low=np.array([l[0] - thresh, l[1] - thresh, l[2] - thresh])
        high=np.array([l[0] + thresh, l[1] + thresh, l[2] + thresh])
        mask1=cv2.inRange(image,low,high)
        res = cv2.bitwise_and(image, image, mask = mask1)
        block=0
        a=i
        b=j
        c=m
        d=n
        k=[]
        b=b+x
        d=d+x
        while(b<256 and d<256):
            k=res[a:c,b:d][0][0]
            black=[0,0,0]
            while((k!=black).all() and b<256 and d<256):
                block=block+1
                b=b+x
                d=d+x
                k=res[a:c,b:d][0][0]
            image[i:m,j+x:(n+((block)*x))]=l
            break
        j=j+x
        n=n+y

    i=i+x
    m=m+y
    j=0
    n=x
image= cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
img = smp.toimage( image )
data1 = np.asarray( img, dtype="int32" )
cv2.imwrite(os.path.join('G:/AI package/datasets/btob/',filename),data1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM