0

我正在使用 Python 进行一些业余图像分析,特别是 Numpy、PIL 和 OpenCV。但是,我对 PIL 的性能不太满意,所以我想将这些代码部分移植到 OpenCV。如果需要,这也将使以后更容易将代码移植到 C/C++。

无论如何,我在移植部分代码时遇到了一些麻烦,希望在这里得到一些帮助。具体来说,我需要更改的部分是:

功能:找出图像中像素最多的红、绿、蓝(R、G、B)强度。目前通过直方图完成。输入图像是 Python PIL 图像的形式。

def bgcalcRGB(img):

    hist = img.histogram()

    R=0;G=0;B=0; avgR=0.; avgG=0.; avgB=0.; Rmax=175+15; Rmin=175-15;
    Gmax=160+15; Gmin=160-15; Bmax=150+15; Bmin=150-15;

    for x in range(Rmin,Rmax,1):
        if hist[x] > avgR: avgR = hist[x]; R = x
    for x in range(256+Gmin,Gmax+256,1):
        if hist[x] > avgG: avgG = hist[x]; G = x - 256
    for x in range(256*2+Bmin,Bmax+256*2,1):
        if hist[x] > avgB: avgB = hist[x]; B = x - 256*2
    return (R,G,B)

功能:作为 RGB 通道的同时阈值。如果某个 RGB 像素在某个范围内(与 R、G 和 B 无关),则其颜色为白色。如果不是,则将其丢弃并涂成黑色。输入图像是 Python PIL 图像。

def mask(low, high):
    return [255 if low <= x <= high else 0 for x in range(0, 256)]

img1 = img.point(mask(val1-uc1,val1+uc1)+mask(val2-uc2,val2+uc2)+mask(val3-uc2,val2+uc2)).convert('L').point([0]*255+[255]).convert('RGB')

所以我需要帮助将上述代码转换为仅使用 OpenCV,或者至少不使用 Python PIL。

谢谢!

4

1 回答 1

1

你应该试试SimpleCV

它基于 Python OpenCV 绑定、scipy、numpy、pygame、PIL。SimpleCV 非常容易学习和实现。

img = Image("filename.jpg")将以 BGR 形式加载图像。

要访问图像的 x、y 像素,color = img[x, y]

得到 cv2.cv.iplimage =>img.getBitmap()

SimpleCV 集成了 scipy 和 numpy 用于快速计算和机器学习模块。它包括 pygame 显示。

要显示图像,img.show()

它还包括各种跟踪功能(Lucas Kanade Tracker、CAMShift 等)、关键点匹配功能(SURF、SIFT、ASIFT、STAR、ORB、MSER 等)和许多其他 blob 功能。

使用 SimpleCV 可以轻松完成基本图像处理和图像数据提取。

于 2012-08-30T16:44:42.310 回答