import cv2
file="face_01.jpg"
img=cv2.imread(file)
imgray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
h,w = imgray.shape
print (imgray[60][39])
Doing so will result in IndexError: index 60 is out of bounds for axis 0 with size 40
.I did this because I wanted to check if the coordinates [60][39] of imgray are black or white.By the way, h=40, w=67.Please tell me the solution.
In OpenCV, the sequence of subscripts in the array (ndarray) is (y, x(,c) instead of (x, y(,c).
This is probably because, given the continuity of memory, y comes first in order to keep the memory in the x direction.
As for the code you asked, the subscripts x and y are reversed, so the access to the image is out of range and the error is displayed, so please change the order of the subscripts and
print (imgray[39][60])
I think I should.
References:
https://stackoverflow.com/questions/19098104/python-opencv2-cv2-wrapper-get-image-size
Numpy stores data in memory in the same order as in C language, so if array[i,j], the memory will be continuous for subscript j (note that Fortran and so on are the opposite).
http://kaisk.hatenadiary.com/entry/2015/02/19/224531
According to Accessing and Modifying pixel values,
It seems that you can get the pixel value below.
px=img[100,100]
I can't check it because I don't have an environment, but
The following may solve the problem.
import cv2
file="face_01.jpg"
img=cv2.imread(file)
imgray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
h,w = imgray.shape
print(imgray[39,60])
© 2024 OneMinuteCode. All rights reserved.