使用 Python 在 OpenCV 中存储

发布于 2024-12-28 08:09:05 字数 7851 浏览 1 评论 0原文

我想找到图像中的轮廓并进一步处理它们,例如在图像上绘制它们。 为此,我有两个函数在不同的线程中运行:

storage = cv.CreateMemStorage(0)
contour = cv.FindContours(inData.content, storage, cv.CV_RETR_EXTERNAL, cv.CV_CHAIN_APPROX_SIMPLE)

每个

while contours:
        bound_rect = cv.BoundingRect(list(contours))
        contours = contours.h_next()

        pt1 = (bound_rect[0], bound_rect[1])
        pt2 = (bound_rect[0] + bound_rect[2], bound_rect[1] + bound_rect[3])
        cv.Rectangle(inImg.content, pt1, pt2, cv.CV_RGB(255,0,0), 1)

函数都在一个循环中运行,一个接一个地处理图像。 当一个函数完成后,它将图像放入缓冲区中,其他函数可以从中获取它。 这种方法有效,只是在结果中,轮廓是在图像中相应图像之前的一到两个图像中绘制的。

我认为这与 OpenCV 的存储有关,但我不明白为什么需要存储以及它的作用

编辑这里还有一些代码:
我的程序是一个基于节点的图像分析软件。
这就是我当前代码的节点图的样子:

                         |---------|    |--------|
|-----|    |-----|------>|Threshold|--->|Contours|--->|-------------|    |------|
|Input|--->|Split|       |---------|    |--------|    |Draw Contours|--->|Output|
|-----|    |-----|----------------------------------->|-------------|    |------|

这是所有节点派生的类:

from Buffer import Buffer
from threading import Thread
from Data import Data
class Node(Thread):

    def __init__(self, inputbuffers, outputbuffers):
        Thread.__init__(self)

        self.inputbuffers = inputbuffers
        self.outputbuffers = outputbuffers
    def getInputBuffer(self, index):
        return self.inputbuffers[index]
    def getOutputBuffer(self, index):
        return self.outputbuffers[index]

    def _getContents(self, bufferArray):
        out = []
        for bufferToGet in bufferArray:
            if bufferToGet and bufferToGet.data:
                out.append(bufferToGet.data)
        for bufferToGet in bufferArray:
            bufferToGet.data = None
        return out
    def _allInputsPresent(self):
        for bufferToChk in self.inputbuffers:
            if not bufferToChk.data:
                return False
        return True
    def _allOutputsEmpty(self):
        for bufferToChk in self.outputbuffers:
            if bufferToChk.data != None:
                return False
        return True


    def _applyOutputs(self, output):
        for i in range(len(output)):
            if self.outputbuffers[i]:
                    self.outputbuffers[i].setData(output[i])

    def run(self):
        #Thread loop <------------------------------------
        while True:
            while not self._allInputsPresent(): pass
            inputs = self._getContents(self.inputbuffers)
            output = [None]*len(self.outputbuffers)
            self.process(inputs, output)
            while not self._allOutputsEmpty(): pass
            self._applyOutputs(output)

    def process(self, inputs, outputs):
        '''
        inputs: array of Data objects
        outputs: array of Data objects
        '''
        pass

节点围绕这些数据对象传递:

class Data(object):

    def __init__(self, content = None, time = None, error = None, number = -1):
        self.content = content #Here the actual data is stored. Mostly images
        self.time = time #Not used yet
        self.error = error #Not used yet
        self.number = number #Used to see if the correct data is put together 

这是节点:

from Node import Node
from Data import Data
import copy
import cv

class TemplateNode(Node):

    def __init__(self, inputbuffers, outputbuffers):

        super(type(self), self).__init__(inputbuffers, outputbuffers)

    def process(self, inputs, outputs):
        inData = inputs[0]
        #Do something with the content e.g.
        #cv.Smooth(inData.content, inData.content, cv.CV_GAUSSIAN, 11, 11)
        outputs[0] = inData

class InputNode(Node):

    def __init__(self, inputbuffers, outputbuffers):
        super(InputNode, self).__init__(inputbuffers, outputbuffers)
        self.capture = cv.CaptureFromFile("video.avi")
        self.counter = 0

    def process(self, inputs, outputs):
        image = cv.QueryFrame(self.capture)
        if image:
            font = cv.InitFont(cv.CV_FONT_HERSHEY_SIMPLEX, 1, 1, 0, 3, 8)
            x = 30
            y = 50
            cv.PutText(image, str(self.counter), (x,y), font, 255)
            outputs[0] = Data(image,None,None,self.counter)
            self.counter = self.counter+1

class OutputNode(Node):

    def __init__(self, inputbuffers, outputbuffers, name):
        super(type(self), self).__init__(inputbuffers, outputbuffers)
        self.name = name

    def process(self, inputs, outputs):
        if type(inputs[0].content) == cv.iplimage:
            cv.ShowImage(self.name, inputs[0].content)
            cv.WaitKey()

class ThresholdNode(Node):

    def __init__(self, inputbuffers, outputbuffers):
        super(type(self), self).__init__(inputbuffers, outputbuffers)

    def process(self, inputs, outputs):
        inData = inputs[0]
        inimg = cv.CreateImage(cv.GetSize(inData.content), cv.IPL_DEPTH_8U, 1);
        cv.CvtColor(inData.content, inimg, cv.CV_BGR2GRAY)
        outImg = cv.CreateImage(cv.GetSize(inimg), cv.IPL_DEPTH_8U, 1);
        cv.Threshold(inimg, outImg, 70, 255, cv.CV_THRESH_BINARY_INV);
        inData.content = outImg
        outputs[0] = inData

class SplitNode(Node):

    def __init__(self, inputbuffers, outputbuffers):
        super(type(self), self).__init__(inputbuffers, outputbuffers)

    def process(self, inputs, outputs):
        inData = inputs[0]
        if type(inData.content) == cv.iplimage:
            imagecpy = cv.CloneImage(inData.content)
            outputs[1] = Data(imagecpy, copy.copy(inData.time), copy.copy(inData.error), copy.copy(inData.number))
        else:
            outputs[1] = copy.deepcopy(inData)
        print

class ContoursNode(Node):

    def __init__(self, inputbuffers, outputbuffers):
        super(type(self), self).__init__(inputbuffers, outputbuffers)

    def process(self, inputs, outputs):
        inData = inputs[0]

        storage = cv.CreateMemStorage(0)
        contours = cv.FindContours(inData.content, storage, cv.CV_RETR_EXTERNAL, cv.CV_CHAIN_APPROX_SIMPLE)
        contoursArr = []
        while contours:
            points = []
            for (x,y) in contours:
                points.append((x,y))
            contoursArr.append(points)
            contours = contours.h_next()

        outputs[0] = Data(contoursArr, inData.time, inData.error, inData.number)
        pass


class DrawContoursNode(Node):

    def __init__(self, inputbuffers, outputbuffers):
        super(type(self), self).__init__(inputbuffers, outputbuffers)

    def process(self, inputs, outputs):
        inImg = inputs[0]

        contours = inputs[1].content

        print "Image start"
        for cont in contours:
            for (x,y) in cont:
                cv.Circle(inImg.content, (x,y), 2, cv.CV_RGB(255, 0, 0))
        print "Image end"
        outputs[0] = inImg

这是主函数。这里创建了所有节点和缓冲区。

from NodeImpls import *
from Buffer import Buffer

buffer1 = Buffer()
buffer2 = Buffer()
buffer3 = Buffer()
buffer4 = Buffer()
buffer5 = Buffer()
buffer6 = Buffer()

innode = InputNode([], [buffer1])
split = SplitNode([buffer1], [buffer2, buffer3])
thresh = ThresholdNode([buffer3], [buffer4])
contours = ContoursNode([buffer4], [buffer5])
drawc = DrawContoursNode([buffer2, buffer5],[buffer6])
outnode = OutputNode([buffer6], [], "out1")

innode.start()
split.start()
thresh.start()
contours.start()
drawc.start()
outnode.start()


while True:
    pass

缓冲区:

class Buffer(object):

    def __init__(self):
        self.data = None

    def setData(self, data):
        self.data = data
    def getData(self):
        return self.data

I want to find contours in an image and further process them e.g. drawing them on the image.
To do that I have two functions running in different threads:

storage = cv.CreateMemStorage(0)
contour = cv.FindContours(inData.content, storage, cv.CV_RETR_EXTERNAL, cv.CV_CHAIN_APPROX_SIMPLE)

and

while contours:
        bound_rect = cv.BoundingRect(list(contours))
        contours = contours.h_next()

        pt1 = (bound_rect[0], bound_rect[1])
        pt2 = (bound_rect[0] + bound_rect[2], bound_rect[1] + bound_rect[3])
        cv.Rectangle(inImg.content, pt1, pt2, cv.CV_RGB(255,0,0), 1)

Each function runs in a loop processing one image after the other.
When a function is done it puts the image in a buffer from which the other function can get it.
This works except that in the result the contours are drawn in the image one or two images before their corresponding image.

I think this has something to do with the storage of OpenCV but I don't understand why the storage is needed and what it does

EDIT Here is some more code:
My program is meant to be a node based image analasys software.
This is how the node graph of my current code looks like:

                         |---------|    |--------|
|-----|    |-----|------>|Threshold|--->|Contours|--->|-------------|    |------|
|Input|--->|Split|       |---------|    |--------|    |Draw Contours|--->|Output|
|-----|    |-----|----------------------------------->|-------------|    |------|

This is the class from which all nodes derive:

from Buffer import Buffer
from threading import Thread
from Data import Data
class Node(Thread):

    def __init__(self, inputbuffers, outputbuffers):
        Thread.__init__(self)

        self.inputbuffers = inputbuffers
        self.outputbuffers = outputbuffers
    def getInputBuffer(self, index):
        return self.inputbuffers[index]
    def getOutputBuffer(self, index):
        return self.outputbuffers[index]

    def _getContents(self, bufferArray):
        out = []
        for bufferToGet in bufferArray:
            if bufferToGet and bufferToGet.data:
                out.append(bufferToGet.data)
        for bufferToGet in bufferArray:
            bufferToGet.data = None
        return out
    def _allInputsPresent(self):
        for bufferToChk in self.inputbuffers:
            if not bufferToChk.data:
                return False
        return True
    def _allOutputsEmpty(self):
        for bufferToChk in self.outputbuffers:
            if bufferToChk.data != None:
                return False
        return True


    def _applyOutputs(self, output):
        for i in range(len(output)):
            if self.outputbuffers[i]:
                    self.outputbuffers[i].setData(output[i])

    def run(self):
        #Thread loop <------------------------------------
        while True:
            while not self._allInputsPresent(): pass
            inputs = self._getContents(self.inputbuffers)
            output = [None]*len(self.outputbuffers)
            self.process(inputs, output)
            while not self._allOutputsEmpty(): pass
            self._applyOutputs(output)

    def process(self, inputs, outputs):
        '''
        inputs: array of Data objects
        outputs: array of Data objects
        '''
        pass

The nodes pass around these Data objects:

class Data(object):

    def __init__(self, content = None, time = None, error = None, number = -1):
        self.content = content #Here the actual data is stored. Mostly images
        self.time = time #Not used yet
        self.error = error #Not used yet
        self.number = number #Used to see if the correct data is put together 

This are the nodes:

from Node import Node
from Data import Data
import copy
import cv

class TemplateNode(Node):

    def __init__(self, inputbuffers, outputbuffers):

        super(type(self), self).__init__(inputbuffers, outputbuffers)

    def process(self, inputs, outputs):
        inData = inputs[0]
        #Do something with the content e.g.
        #cv.Smooth(inData.content, inData.content, cv.CV_GAUSSIAN, 11, 11)
        outputs[0] = inData

class InputNode(Node):

    def __init__(self, inputbuffers, outputbuffers):
        super(InputNode, self).__init__(inputbuffers, outputbuffers)
        self.capture = cv.CaptureFromFile("video.avi")
        self.counter = 0

    def process(self, inputs, outputs):
        image = cv.QueryFrame(self.capture)
        if image:
            font = cv.InitFont(cv.CV_FONT_HERSHEY_SIMPLEX, 1, 1, 0, 3, 8)
            x = 30
            y = 50
            cv.PutText(image, str(self.counter), (x,y), font, 255)
            outputs[0] = Data(image,None,None,self.counter)
            self.counter = self.counter+1

class OutputNode(Node):

    def __init__(self, inputbuffers, outputbuffers, name):
        super(type(self), self).__init__(inputbuffers, outputbuffers)
        self.name = name

    def process(self, inputs, outputs):
        if type(inputs[0].content) == cv.iplimage:
            cv.ShowImage(self.name, inputs[0].content)
            cv.WaitKey()

class ThresholdNode(Node):

    def __init__(self, inputbuffers, outputbuffers):
        super(type(self), self).__init__(inputbuffers, outputbuffers)

    def process(self, inputs, outputs):
        inData = inputs[0]
        inimg = cv.CreateImage(cv.GetSize(inData.content), cv.IPL_DEPTH_8U, 1);
        cv.CvtColor(inData.content, inimg, cv.CV_BGR2GRAY)
        outImg = cv.CreateImage(cv.GetSize(inimg), cv.IPL_DEPTH_8U, 1);
        cv.Threshold(inimg, outImg, 70, 255, cv.CV_THRESH_BINARY_INV);
        inData.content = outImg
        outputs[0] = inData

class SplitNode(Node):

    def __init__(self, inputbuffers, outputbuffers):
        super(type(self), self).__init__(inputbuffers, outputbuffers)

    def process(self, inputs, outputs):
        inData = inputs[0]
        if type(inData.content) == cv.iplimage:
            imagecpy = cv.CloneImage(inData.content)
            outputs[1] = Data(imagecpy, copy.copy(inData.time), copy.copy(inData.error), copy.copy(inData.number))
        else:
            outputs[1] = copy.deepcopy(inData)
        print

class ContoursNode(Node):

    def __init__(self, inputbuffers, outputbuffers):
        super(type(self), self).__init__(inputbuffers, outputbuffers)

    def process(self, inputs, outputs):
        inData = inputs[0]

        storage = cv.CreateMemStorage(0)
        contours = cv.FindContours(inData.content, storage, cv.CV_RETR_EXTERNAL, cv.CV_CHAIN_APPROX_SIMPLE)
        contoursArr = []
        while contours:
            points = []
            for (x,y) in contours:
                points.append((x,y))
            contoursArr.append(points)
            contours = contours.h_next()

        outputs[0] = Data(contoursArr, inData.time, inData.error, inData.number)
        pass


class DrawContoursNode(Node):

    def __init__(self, inputbuffers, outputbuffers):
        super(type(self), self).__init__(inputbuffers, outputbuffers)

    def process(self, inputs, outputs):
        inImg = inputs[0]

        contours = inputs[1].content

        print "Image start"
        for cont in contours:
            for (x,y) in cont:
                cv.Circle(inImg.content, (x,y), 2, cv.CV_RGB(255, 0, 0))
        print "Image end"
        outputs[0] = inImg

This is the main function. Here all the nodes and buffers are created.

from NodeImpls import *
from Buffer import Buffer

buffer1 = Buffer()
buffer2 = Buffer()
buffer3 = Buffer()
buffer4 = Buffer()
buffer5 = Buffer()
buffer6 = Buffer()

innode = InputNode([], [buffer1])
split = SplitNode([buffer1], [buffer2, buffer3])
thresh = ThresholdNode([buffer3], [buffer4])
contours = ContoursNode([buffer4], [buffer5])
drawc = DrawContoursNode([buffer2, buffer5],[buffer6])
outnode = OutputNode([buffer6], [], "out1")

innode.start()
split.start()
thresh.start()
contours.start()
drawc.start()
outnode.start()


while True:
    pass

The buffer:

class Buffer(object):

    def __init__(self):
        self.data = None

    def setData(self, data):
        self.data = data
    def getData(self):
        return self.data

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凡间太子 2025-01-04 08:09:05

我认为这与OpenCV的存储有关,但我不明白为什么需要存储以及它的作用

存储只是一个保存结果的地方。 OpenCV 是一个 C++ 库,依赖于手动内存分配,C++ 风格。 Python 绑定只是它的一个薄包装,并且不太Pythonic。这就是为什么您必须手动分配存储,就像在 C 或 C++ 中那样。

我有两个函数在不同的线程中运行
...
这种方法有效,只是在结果中,轮廓是在图像中相应图像之前的一到两个图像中绘制的。

我假设您的线程没有正确同步。这个问题不太可能与 OpenCV 有关,而是与您拥有哪些功能、它们使用和传递哪些数据以及您如何在它们之间共享数据有关。

简而言之,请将您的代码发布在您创建线程并调用这些函数的位置,以及 inImginDatacontourcontours 的位置存储 被访问或修改。

I think this has something to do with the storage of OpenCV but I don't understand why the storage is needed and what it does

Storage is just a place to keep the results. OpenCV is a C++ library, and relies on manual memory allocation, C++ style. Python bindings are just a thin wrapper around it, and are not very pythonic. That's why you have to allocate storage manually, like if you did it in C or in C++.

I have two functions running in different threads
...
This works except that in the result the contours are drawn in the image one or two images before their corresponding image.

I assume your threads are not properly synchronized. This problem is not likely to be related to OpenCV, but to what functions you have, what data they use and pass around, and how you share the data between them.

In short, please post your code where you create threads and call these functions, as well where inImg, inData, contour, contours and storage are accessed or modified.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文