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

Hello. How to implement in Python such a scheme?
p.s Now the transfer of the option (img) in the constructor of class A is not by reference, by value, and when you call the painter no image, because is not stored in the img link

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.((1,1,1),np.uin8)
 self.painter = A(self.img)

 def getImg(self):
 whille True:
 _, self.img = cv2.read()
 self.painter.show()
April 19th 20 at 12:47
2 answers
April 19th 20 at 12:49
From the example it is not clear what is your problem, but lost the indentation, well, painter (maybe pointer?) need to add self if you want so that it is available between the methods.
The question how to pass a pointer to the constructor of class A is now passed, not a pointer, so when you call the painter draws a black square instead of image - kayli_Ku commented on April 19th 20 at 12:52
@kayli_Ku, if I understand correctly your wishlist
class A:
 def show(self, img):
cv2.imshow(img)
cv2.waitKey(33)

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

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

ie not inside A need to have this img well, plus learn from In dependence from class A, and to transfer this dependence is only on the stage, create an instance B(A()) - maureen.Dicki commented on April 19th 20 at 12:55
@maureen.Dicki, this is a simplified example. I was in the classroom it is necessary to initialize objects of other classes and use their methods. In your example copies the array that will influence performance in the actual task. I need to pass it a pointer as in C-like languages - kayli_Ku commented on April 19th 20 at 12:58
@kayli_Ku, no, in my example array copy. - maureen.Dicki commented on April 19th 20 at 13:01
@maureen.Dicki, self.painter.show(self.img) - there is a copy to self.img - tuple - kayli_Ku commented on April 19th 20 at 13:04
@kayli_Ku, well, in any case, in python all variables are pointers, but it is not like pointers in C, then the basis of mutable vs immutable, and variables is not that si - maureen.Dicki commented on April 19th 20 at 13:07
@kayli_Ku, there is no copy until you start to change these data. - maureen.Dicki commented on April 19th 20 at 13:10
@maureen.Dicki, it would've worked in my example, you are contradicting yourself. If all the signs I'd passed in the constructor and in the method of the painter spoke to him. - kayli_Ku commented on April 19th 20 at 13:13
@kayli_Ku, in your example wrong, I say that there is almost no si.
will try to simplistically explain how it works in python
1. np.((1,1,1),np.uin8) - that's "memory"
2. self.img = np.((1,1,1),np.uin8) - this is eksemplar.img points to the "memory location"
3. A(self.img) - this will create the class object A where PCs.img points to the "memory location"
4 _, self.img = cv2.read() - this is eksemplar.img points to the new "memory"
5. self.painter.show() - this is use PCs.img, which is still pointing to the old "memory" - maureen.Dicki commented on April 19th 20 at 13:16
@maureen.Dicki, Yes OK.. but I have already allocated memory for self.img in the constructor of class B. cv2.read only saves the new data, he does not allocate every time a new memory in the heap. - kayli_Ku commented on April 19th 20 at 13:19
@maureen.Dicki, how then to create a variable self.img class and allocate memory to cv2.read recorded in the same memory area? - kayli_Ku commented on April 19th 20 at 13:22
@kayli_Ku, to read docks cv2, there's nothing in there about it, but I suspect that any way, there will always be a new area - maureen.Dicki commented on April 19th 20 at 13:25
@maureen.Dicki, Yes, there is no case in opencv and Python. Well, I wrote that already implemented what you want. But it's more like a crutch. I would like to understand why - kayli_Ku commented on April 19th 20 at 13:28
@kayli_Ku, because we do not need to work with python as with C - maureen.Dicki commented on April 19th 20 at 13:31
@maureen.Dicki, you would already have tried to run the example of the author, there is a stupid error in the call to cv2.imshow :D - Norval.Watsica commented on April 19th 20 at 13:34
@Norval.Watsica, not immediately in the other case, cv2.read() changes self.img class B and class A remains of the original img - maureen.Dicki commented on April 19th 20 at 13:37
@maureen.Dicki, the Program the author dies on syntax error. In class B, too, there is again a syntax error. Yet you continue to talk about. What is "np."? Function cv2 too.read() is not defined. Where the main () function?

After him, and do not see the connection classes. There should be an inheritance or something? - Norval.Watsica commented on April 19th 20 at 13:40
@Norval.Watsica, he's just a clumsy example here wrote (the first version was even worse), once he wants to pass in a reference to self.img from in, then In V. getImg update the data, and the class And is responsible for the output. - maureen.Dicki commented on April 19th 20 at 13:43
@Norval.Watsica, composition, painter is an instance of a - maureen.Dicki commented on April 19th 20 at 13:46
@maureen.Dicki, About Your last comment, Yes, I on Your look. - Norval.Watsica commented on April 19th 20 at 13:49
@maureen.Dicki,
he wants a one-time transfer in As a reference to the self.img from in, then In V. getImg update the data, and the class And is responsible for the output.


Or inheritance, to use or let about "design patterns" reads. Or here's a minimal version:

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):
 self.painter = A()

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


if __name__ == '__main__':
 b = b()
 b.getimg()
- Norval.Watsica commented on April 19th 20 at 13:52
@Norval.Watsicadirectly to the subject matter has nothing to do, well, you have here also a mistake in the design: too much dependence, why should the class know how to create And? He just needs to know its interface, i.e. you simply call its show method and the implementation of it can be any, So before his creation, made from class B. - maureen.Dicki commented on April 19th 20 at 13:55
April 19th 20 at 12:51
Enough to discuss all sorts of crap. The author's error in the function call imshow.

Here's the working version:

import cv2


class A:

 def __init__(self, img):
 self.img = img

 def show(self):
 cv2.imshow( 'image', self.img)
cv2.waitKey(3300)


if __name__ == '__main__':
 img = cv2.imread('Capture.JPG')

 a = a(img)
 a.show()

Find more questions by tags OpenCVPython