python - 使用numpy和cv2处理大型二进制图像数组

标签 python arrays opencv numpy

我的代码如下:

import cv2; import numpy as np

class MyClass:
    def __init__(self,imagefile):
        self.image = cv2.imread(imagefile)

        #image details
        self.h,self.w = self.image.shape[:2]
        #self.bPoints, self.wPoints = np.array([[0,0]]),np.array([[0,0]])
        self.bPoints, self.wPoints = [],[]

        #CAUTION! Points are of the form (y,x)
        # Point filtering
        for i in xrange(self.h):
            for j in xrange(self.w):
                if self.th2.item(i,j) == 0:
                    #self.bPoints = np.append([[i,j]], self.bPoints, axis=0)
                    self.bPoints.append((i,j))
                else:
                    self.wPoints.append((i,j))
                    #self.wPoints = np.append([[i,j]], self.wPoints, axis=0)

        #self.bPoints = self.bPoints[:len(self.bPoints) - 1]
        #self.wPoints = self.wPoints[:len(self.wPoints) - 1]
        self.bPoints, self.wPoints = np.array(self.bPoints), np.array(self.wPoints)

我想找到白色和黑色点并分开。我评论了通过numpy显示可能的(但非常慢)解决方案的行。您能推荐我一个更好更快的解决方案吗?如果您愿意,我将不胜感激!

谢谢

最佳答案

我假设self.th2是一个numpy数组。如果不是这种情况,则可能需要进行一些调整。基本上,这使用np.where函数来确定所有索引,这些索引都是0255

import cv2; import numpy as np

class MyClass:
    def __init__(self,imagefile):
        self.image = cv2.imread(imagefile)

        #image details
        self.h,self.w = self.image.shape[:2]
        #self.bPoints, self.wPoints = np.array([[0,0]]),np.array([[0,0]])
        self.bPoints, self.wPoints = [],[]

        #CAUTION! Points are of the form (y,x)
        # use the np.where method instead of a double loop. 
        # make sure self.th2 is a numpy array
        indx = np.where(self.th2==0)
        for i,j in zip(indx[0], indx[1]):
            self.bPoints.append((i,j))

        indx = np.where(self.th2==255)
        for i,j in zip(indx[0], indx[1]):
            self.wPoints.append((i,j))

        # Point filtering
        #for i in xrange(self.h):
        #    for j in xrange(self.w):
        #        if self.th2.item(i,j) == 0:
        #            #self.bPoints = np.append([[i,j]], self.bPoints, axis=0)
        #            self.bPoints.append((i,j))
        #        else:
        #            self.wPoints.append((i,j))
        #            #self.wPoints = np.append([[i,j]], self.wPoints, axis=0)

        #self.bPoints = self.bPoints[:len(self.bPoints) - 1]
        #self.wPoints = self.wPoints[:len(self.wPoints) - 1]
        self.bPoints, self.wPoints = np.array(self.bPoints), np.array(self.wPoints)

关于python - 使用numpy和cv2处理大型二进制图像数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25000924/

相关文章:

python - 有没有比使用 N^2 循环更快的插入 N^2 项的方法?

python - "tf.train.replica_device_setter"是如何工作的?

javascript - 根据两个属性的值对数组中的对象进行排序

javascript - 将 DOM 元素转换回 HTML

opencv - 使用vector <vector <KeyPoint >>进行OpenCV,SIFT计算

opencv - 使用opencv旋转矩阵

python - 将 linspace 向量发送到函数会使该向量在函数启动之前全部为零

python - 如何从 aiohttp.web 服务器返回 HTML 响应?

javascript - 使用 for 循环 JS 遍历数组

python - 如何使用原始图像(.rgb 格式)创建视频并在 python 中添加音频?