I am trying to do simple prewitt edge detection on an image and output 2 images with horizontal and vertical edges. For some reason, whenever the output value should be negative (for example -3), my output array ends up with 256 minus that value (253) giving me a huge amount of background. I tried adding these if/elif/else statements but that helped not at all. I am using the code below and clueless as to what's going wrong:
for i in range(1,(len(os.listdir(images)))):
image=plt.imread(images+(os.listdir(images)[i]))
if len(image.shape)>2:
img=image[:,:,0]
else:
img=image
imgC=deepcopy(img)
imgD=deepcopy(img)
imgE=deepcopy(img)
for x in range(1,img.shape[1]-2):
for y in range(1,img.shape[0]-2):
avgA=(img[(y-1),x]+img[(y-1),(x+1)]+img[(y-1),(x-1)])
avgC=(img[(y+1),x]+img[(y+1),(x+1)]+img[(y+1),(x-1)])
avgT=(img[(y-1),(x-1)]+img[y,(x-1)]+img[(y+1),(x-1)])
avgB=(img[(y-1),(x+1)]+img[y,(x+1)]+img[(y+1),(x+1)])
if (avgA-avgC)<0:
imgC[y,x]=0
elif (avgA-avgC)>255:
imgC[y,x]=255
else:
imgC[y,x]=(avgA-avgC)
if (avgT-avgB)<0:
imgD[y,x]=0
elif (avgT-avgB)>255:
imgD[y,x]=255
else:
imgD[y,x]=(avgT-avgB)
imgE[y,x]=math.sqrt(((imgC[y,x]**2)+(imgD[y,x]**2)))