N
N
Nik872020-04-19 05:25:34
Python
Nik87, 2020-04-19 05:25:34

How to implement in python passing opencv image "pointer" to the constructor of another class?

Hello. How to implement such a scheme in python?
ps Now passing the parameter (img) to the constructor of class A goes by reference, but when the painter method is called, there is no image, because another reference is overwritten in the img variable of class B

class A:
  def __init__(self, img):
   self.img = img
  def show(self):
   cv2.imshow(self.img)
   cv2.waitKey(33)

class B:
  def __init__(self):
   ...
   self.img = np.array((1,1,1),np.uin8)
   self.painter = A(self.img)

  def getImg(self):
    whille True:
       _, self.img = self.cap.read()
      self.painter.show()

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dr. Bacon, 2020-04-19
@bacon

From the example it is not clear what your problem is, except for the lost indents, well, you need to add self to the painter (maybe pointer?) if you want it to be available between methods.

A
Andy_U, 2020-04-19
@Andy_U

After discussion, I corrected the answer:

from typing import Any

import cv2
import numpy


class A:

    @staticmethod
    def show(img: numpy.ndarray) -> None:
        cv2.imshow('image', img)
        cv2.waitKey(3300)


class B:

    def __init__(self, painter: Any):
        self.painter = painter

    def getimg(self) -> None:
        while True:
            self.painter.show(cv2.imread('Capture.JPG'))


if __name__ == '__main__':
    b = B(A())
    b.getimg()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question