Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7175913
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T16:24:02+00:00 2026-05-28T16:24:02+00:00

I want to find contours in an image and further process them e.g. drawing

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T16:24:03+00:00Added an answer on May 28, 2026 at 4:24 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two contours and I want to check the relation between them (if
I have an image where I want to find contours but the contours in
I have drawn a circle on my image, and I want to find any
I have some scattered 3D points (2d solution is sufficient). I want find different
I want to find the centroid of each contour.for that i take some sample
I want to find the first item in a sorted vector that has a
I want to find all items in one collection that do not match another
I want to find all the required fields on my form that are within
I want to find the length of a song in seconds so that I
If I want find the differences between two directory trees, I usually just execute:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.