2

I am trying to split an openCV frame image that I get from an input video stream

_, frame = cap.read()

into several smaller images and store them into an array. I don't know how many smaller images I will have beforehand, for example: I could split the image into 4 smaller images, or 8, 16 etc.

I want to create a function that allows me to display any arbitrary combination of the smaller images. Currently, it doesn't matter to me if they're being displayed in two separate windows or the same one (even though I would prefer them to be displayed in separate windows).

What I tried obviously doesn't work, looping over the list only displays the last image in the list:

# GridCells is the List that contains all the smaller images
def showCells(self):
    for c in self.GridCells:
        c.showC()

Where showC() is:

def showC(self):
    cv2.imshow('cell',self.image)

As said I don't know how many smaller images I will have beforehand, hence having arbitrarily many cv2.imshow() statements is not a solution.

Thank you for your time!

Adam Jaamour
  • 1,326
  • 1
  • 15
  • 31
Ahmad Moussa
  • 876
  • 10
  • 31

2 Answers2

1

You are only displaying the last image because you are giving all your images the same window name, here:

cv2.imshow('cell',self.image)

If you give each image a different name ('cell1', 'cell2', 'cell3' etc) they should show up at the same time.

PlinyTheElder
  • 1,454
  • 1
  • 10
  • 15
1

Try this to make OpenCV create a new window for each image, where each window has a different name.

You can use the enumerate() function, which will be useful to have different window names, and the string formatter format() to quickly name the different windows using the enumerator passed to your showC function.

# GridCells is the List that contains all the smaller images
def showCells(self):
    for i, c in enumerate(self.GridCells):
        c.showC(i)

def showC(self, i):
    cv2.imshow("cell{}".format(i),self.image)
Adam Jaamour
  • 1,326
  • 1
  • 15
  • 31
  • 1
    Up-voted your answer, but still need more reputation for it to count. Thank you kind sir, you just taught me about enumerate and string formatters. It worked like a charm! – Ahmad Moussa Apr 17 '19 at 14:25