Reputation: 11
I am trying to read RGB pixels from a numpy ndarray
type image. I implemented it in 2 class files. To search for histograms and look for momen. The first process will go through a histogram search first (calling the class histogram
), then the process continues by searching for the momen (calling the class momen
). In both of these classes there is each process of reading the RGB pixels of the input image. I access RGB pixels using the following code:
def getbyte(self, gambar):
for i in range(0,gambar.shape[0]):
for j in range(0,gambar.shape[1]):
self.p.append(gambar[i,j])
self.flat = [i for sets in self.p for i in sets]
return self.flat
Then I run the program. If you see from the flow process, then the histogram will be processed before momen. When I run, when I look at the console
(I use spyder IDE
), the process has arrived at the momen search line, it indicates that the histogram search has succeeded, right?
Now then what I want to ask, when searching for moments is executed, I get an error in the reading of the RGB pixel of the image, as follows:
self.flat = [i for sets in self.pMoment for i in sets]
TypeError: 'numpy.uint8' object is not iterable
Strangely, when searching for a histogram with the exact pixel capture code, the error does not appear. But why when searching for the momen this code has a problem and an error appears? What error is that?
Please help
Upvotes: 0
Views: 104
Reputation: 26
assuming the self.p
attribute is a list (my assumption is based on theappend ()
method used) then the error code is in the loop adding a value to the self.p
attribute. Supposedly, before flattening, attribute self.p should be a list of list. However, in the code self.p is still a list of ints.
So what needs to be done is to temporarily store the pixel values in the image in each row using the new variables. Then this variable will be added (append
) to self.p
.
The code snippet will look like this:
def getbyte (self, image):
for i in range (0, image.shape [0]):
rowPixel = []
for j in range (0, images.shape [1]):
rowPixel.append (image [i, j])
self.p.append (rowPixel)
self.flat = [i for sets in p for i in sets]
return self.flat
I have tried the code above using a 2-dimensional image variable of type np.ndarray
. Because of my assumption you are doing 2 nested loops.
Upvotes: 1