user12686663
user12686663

Reputation:

Can't I check the entire or specific pixel value of the image array in cv2.imshow()?

MATLAB allows me to check the matrix component values of each variable immediately, so it is easy to check the values of each pixel when I call up the image as imread.

When I click on the image on imshow, I can see the coordinates and the pixel values of the coordinates right away, but I don't know how to check the values when I switch to Python.

I can't click an image, I can't see an array...

I heard that you use Python a lot for image processing, so I try to study, but it seems more uncomfortable than MATLAB. I think it's because I don't know how to use Python, yet.

Upvotes: 1

Views: 4364

Answers (2)

furas
furas

Reputation: 142759

You can use cv2 to read it - imread() - display it and see coordinates and color - imshow()` - or display and seletec region - selectROI() - which you want to get as array.

The only problem is that it keep image as BGR instead of RGB so you have to convert it when you want to use with other modules - like Matplotlib, imageio, PIL/pillow or any GUI framework.


On the bottom it shows pixel coordinates and color.

On top it has buttons to scroll, zoom, etc. but on my computer it doesn't shows icons on buttons.

enter image description here

enter image description here

I used imageio to read image directly from internet

import cv2
import imageio

img = imageio.imread('https://upload.wikimedia.org/wikipedia/en/7/7d/Lenna_%28test_image%29.png?download')
img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
#img = cv2.imread('lenna.png')

cv2.imshow('window', img)
cv2.waitKey(0)
cv2.destroyAllWindows()

Using selectROI or selectROIs you can select region(s) in window, press Space or Enter and you can get it as array

enter image description here

import cv2
import imageio

img = imageio.imread('https://upload.wikimedia.org/wikipedia/en/7/7d/Lenna_%28test_image%29.png?download')
img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
#img = cv2.imread('lenna.png')

region = cv2.selectROI('window', img)
cv2.waitKey(0)
cv2.destroyAllWindows()

x,y,w,h = region
new_img = img[y:y+h,x:x+w]

cv2.imshow('window', new_img)
cv2.waitKey(0)
cv2.destroyAllWindows()

Upvotes: 2

HansHirse
HansHirse

Reputation: 18925

Use Matplotlib then, if you want to have some more MATLAB look and feel. Here's a small example incorporating OpenCV for the image reading:

import cv2
from matplotlib import pyplot as plt

# Read image via OpenCV
img = cv2.imread('path/to/your/image.png')

# Attention: OpenCV uses BGR color ordering per default whereas
# Matplotlib assumes RGB color ordering!
plt.figure(1)
plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
plt.show()

Here's a screenshot of the resulting window (Windows 10):

Screenshot

Have a look at the bottom right corner. You can't see the cursor in the screenshot, but it is set to the upper left corner of the hat (x = 120, y = 5), and the RGB value correctly indicates a (mainly) red color.

In general: Do some search on "Python for MATLAB users"! There are plenty of tutorials focussing on that!

Hope that helps!

-----------------------
System information
-----------------------
Python:      3.8.1
Matplotlib:  3.2.0rc1
OpenCV:      4.1.2
-----------------------

Upvotes: 2

Related Questions