0
import cv2
import numpy as np

big_list = []
great_list=[]
n = 28
for i in range(n):
    big_list.append([255,255,255])



for i in range(28):
    great_list.append(big_list)
    


array=np.array(great_list)
h, w, c = array.shape


array1=np.array(great_list)
h, w, c = array.shape

array2=np.array(great_list)
h, w, c = array.shape


b=0

for i in range(h):
    for j in range(w):    
        for k in range(c):
            array[i,j,k]=255-5*b
    b+=1




for k in range(c):
    for i in range(h):
        for j in range(w):
            if array1[i,j,k]<150:
                array1[i,j,k]=0

            



for k in range(c):
    for i in range(h):
        for j in range(w):
            if array2[i,j,k]>200:
                array2[i,j,k]=255

How can ı show these 3 arrays as images in opencv python?

I just create array? Should I use the imshow method?

I tried to show its as image, but I get this error:

cv2.error: OpenCV(4.4.0) C:/Users/appveyor/AppData/Local/Temp/1/pip-req-build-zsozjuva/opencv/modules/highgui/src/precomp.hpp:137: error: (-215:Assertion failed) src_depth != CV_16F && src_depth != CV_32S in function 'convertToShow'
Red
  • 26,798
  • 7
  • 36
  • 58
  • does [this](https://stackoverflow.com/a/2659378/11803249) answer your question? [this one](https://stackoverflow.com/a/26687038/11803249) can help too. check them out – Shoaib Mirzaei Nov 18 '20 at 15:58
  • I've seen this but couldn't understand where to write my code –  Nov 18 '20 at 16:01

3 Answers3

0

you can use Image from PILL as follows

import cv2
import numpy as np
from PIL import Image

big_list = []
great_list=[]
n = 28
for i in range(n):
    big_list.append([255,255,255])

for i in range(28):
    great_list.append(big_list)
    
array=np.array(great_list)
h, w, c = array.shape

array1=np.array(great_list)
h, w, c = array.shape

array2=np.array(great_list)
h, w, c = array.shape

b=0

for i in range(h):
    for j in range(w):    
        for k in range(c):
            array[i,j,k]=255-5*b
    b+=1

for k in range(c):
    for i in range(h):
        for j in range(w):
            if array1[i,j,k]<150:
                array1[i,j,k]=0

for k in range(c):
    for i in range(h):
        for j in range(w):
            if array2[i,j,k]>200:
                array2[i,j,k]=255

img = Image.fromarray(array, 'RGB')
img.show()
img = Image.fromarray(array1, 'RGB')
img.show()
img = Image.fromarray(array2, 'RGB')
img.show()
Shoaib Mirzaei
  • 512
  • 4
  • 11
0

Use fromarray in PIL to create Image before show image. Example:

image = Image.fromarray(array, 'RGB')
image .show()

Thank you

unique
  • 128
  • 7
0

For loop in OpenCV is a pretty inefficient thing. Try to avoid it whenever you can.

To plot an np.array simplest thing is to use matplotlib.

I didn't get what you are trying to do with the for loops but here is a way to plot

img1 = np.zeros((row, col, dim), dtype=np.uint8)
img2 = 255*np.ones((row, col, dim), dtype=np.uint8)
img3 = 255*np.eye((row, col, dim), dtype=np.uint8)

import matplotlib.pyplot as plt

plt.subplot(311)
plt.imshow(img1)
plt.subplot(312)
plt.imshow(img2)
plt.subplot(313)
plt.imshow(img3)
plt.show()

To change the color of the first array you have, you can use this logic, that would be a lot more efficient:

array = np.zeros((row, col, dim), dtype=np.uint8)
for i in range(h):
    array[i,:,:]=255-5*i

You can do the rest I think.

smttsp
  • 4,011
  • 3
  • 33
  • 62