不确定为什么我要获得索引的轴是轴的范围,而openCV的尺寸误差



我在OpENCV中进行一些图像处理,并且获得索引的尺寸不超出尺寸误差的轴。

我尝试添加

if(i+1 < len(img2) and j+1 < len(img2[0])):

确保我们处于数组中的范围,但是它只是跳过我相信的数组的每个元素。

我尝试删除0并将其制成

for i in range(imgCol):

样式循环。

我还试图减少数组的长度

for i in range(imgCol-1):

但是错误仍然存在。

这是我当前的代码。

img2 = cv2.imread('v2.jpg')
imgRow = img2.shape[0]
imgCol = img2.shape[1]
for i in range(0,imgCol):
    for j in range(0,imgRow):
        if ( img2[i,j,0] == 11 and img2[i,j,1] == 2 and img2[i,j,2] == 12):
            '''do something'''

当我这样运行时(如Udonn00dle所建议的(反转行和col时,我没有任何错误。我不确定您的形状问题,因为我没有您的图像

#importing a random image
from PIL import Image
import urllib.request
import numpy as np
URL = 'http://www.w3schools.com/css/trolltunga.jpg'
with urllib.request.urlopen(URL) as url:
    with open('temp.jpg', 'wb') as f:
        f.write(url.read())
img = Image.open('temp.jpg')
#the beginning of the code
img2 = np.asarray(img)

imgRow = img2.shape[0]
imgCol = img2.shape[1]
#inverted imgRow and imgCol
for i in range(0,imgRow):
    for j in range(0,imgCol):
        if ( img2[i,j,0] == 11 and img2[i,j,1] == 2 and img2[i,j,2] == 12):
            '''do something'''

我相信错误在此行中:

if ( img2[i,j,0] == 11 and img2[i,j,1] == 2 and img2[i,j,2] == 12):

我认为您正在尝试浏览每个像素,如果其值为(11, 2, 12),那么请执行操作。图像是一个3维数组,而不是使用img2[i, j, 0]访问该值,而是可以尝试img[i][j][0]


编辑以总结注释:

  • 您可能具有rowcol相反。
  • 对于故障拍摄的另一个选择是添加一个读取标志cv2.IMREAD_COLOR,以强迫OPENCV以BGR格式读取图像,尽管这是默认值。

最新更新