简体   繁体   English

我正在尝试将python代码应用于目录中的所有文件,但这给我一个错误

[英]I am trying to apply a python code to all the files in a directory but it gives me a error

I am trying to apply a python code to all the files in a directory but it gives me a error: 我正在尝试将python代码应用于目录中的所有文件,但这给我一个错误:

test_image = cv2.imread(sys.argv[1],0)
   IndexError: list index out of range 

I dont know what to change I tried few things but it does not help so if someone can help with this that would be great. 我不知道要更改什么,我尝试了一些尝试,但没有帮助,因此如果有人可以提供帮助,那将是很棒的。 And using stackoverflow for the first time, just to see how it works. 并第一次使用stackoverflow,只是为了了解它是如何工作的。

import sys
import cv2
import os
import numpy as np
from utils import pointsInsideCircle, compare, zigzag
from math import pi as PI

filepath = os.path.join("/Users/ssm/Desktop/X/1/Original Images",     "*.tif")
W   = 8             #block size for comparision
Dsim    = 0.1           #threshold for symmetry
Nd  = 25            #nearest block

quadrants_points = pointsInsideCircle(W/4)  #(i,j) position of blocks      which are partially/completely inside circle of radius W/2
zigzag_points = zigzag(W/2)
test_image = cv2.imread(sys.argv[1],0)
height,width = test_image.shape[:2]
#print (height,width)
vectors_list = []
for j in range(0,height-W+1):
    for i in range(0,width-W+1):
        block = test_image[j:j+W,i:i+W]
        dct_block =  cv2.dct(np.float32(block))
        feature_block = [[],[],[],[]]
        for index,coeff_list in enumerate(zigzag_points):
            for coeff in coeff_list:
                 feature_block[index].append(dct_block[coeff[0],coeff[1]])

        feature_block_np = np.array(feature_block)

        feature_vector = []
        for quadrant,points in quadrants_points.iteritems():
            summ = 0
        for point in points:
            summ = summ + feature_block_np[point[0],point[1]]
        feature_vector.append(summ/PI)
    vectors_list.append(np.array(feature_vector))

vectors_list2 = cv2.sort(np.array(vectors_list),cv2.SORT_EVERY_ROW)
print "vectors calculated"
import json
with open('data.json', 'w') as outfile:
    json.dump(vectors_list2.tolist(), outfile)


i=0
blocks = []
for i in range(0,len(vectors_list)):
    if i%width == 0:
        print i/width
    posA = [i/width,i%width]
    j = i+1
    for j in range(i+1,len(vectors_list)):
        posB = [j/width,j%width]
        if compare(vectors_list[i],vectors_list[j],posA,posB,Dsim,Nd):
            print (posA,posB)
            blocks.append([posA,posB])

output_image = cv2.imread(sys.argv[1],1)
for block in blocks:
    x1 = block[0][0]
    x1_8 = block[0][0]+W
    y1 = block[0][1]
    y1_8 = block[0][1]+W

    output_image[x1:x1_8,y1:y1_8] = [0,0,255]

    x2 = block[1][0]
    x2_8 = block[1][0]+W
    y2 = block[1][1]
    y2_8 = block[1][1]+W

    output_image[x2:x2_8,y2:y2_8]=[0,255,0]

cv2.imwrite("output.jpg",output_image)


print "feature vectors extracted"
test_image = cv2.imread(sys.argv[1],0)

is checking the list provided by the commandline for a file name. 正在检查命令行提供的列表中的文件名。 For example if you invoked this script with: 例如,如果您使用以下命令调用此脚本:

$python myprog.py afilename.xxx

sys.argv would be ['myprog', 'afilename.xxx'] , and this imread line would load an image from afilename.xxx . sys.argv将为['myprog', 'afilename.xxx'] ,并且此未imread行将从afilename.xxx加载图像。

If you don't provide that filename, sys.argv will only have the script name, and sys.argv[1] will raise this error. 如果不提供该文件名,则sys.argv仅具有脚本名称,而sys.argv[1]将引发此错误。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我正在尝试使用 python 库 elementtree 在某些 xml 文件中找到所有具有标签“typedef”的元素,但它给了我一个空数组 - I am trying to find all the element that have the tag "typedef" in certain xml files using python library elementtree but it gives me an empty array 我正在尝试调试给定的python代码,但给出了给定的错误 - I am trying to debug python code given but it gives the error as given 试图点击按钮给我一个错误。 我正在使用 repl.it python 环境 - Trying to click on button gives me an error. I am using repl.it python environment 当我尝试运行代码时,它会出错 - When I am trying to run the code it gives error 我的代码给我一个错误“没有这样的文件或目录” - My code gives me an error "no such file or directory" 我正在尝试编写Python脚本以在目录中打印文件列表 - I am trying to write a Python Script to Print a list of Files In Directory 我正在尝试让 Selenium 谷歌搜索点击,但它总是给我一个错误 - I am trying to make Selenium google search click, but it always gives me an error 我试图让 Selenium 点击第一个谷歌结果,但它总是给我一个错误 - I am trying to make Selenium click on the first google result, but it always gives me an error 我试图遍历列中的每个元素并单击它,但它给了我错误。 “找不到元素” - I am trying to iterate on each element in column and click on it, but it gives me error. "can not locate the element" 我正在尝试使 GET 方法显示网络服务器目录中的所有文件 - I am trying to make the GET method to show all files in webserver directory
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM