0
def detect_edges (image: Image, threshold: float) -> Image: 
    
    new_image = copy(image)
    
    for x,y, (r,g,b) in image:
        
        brightness = (r+g+b)/3
        
        if abs(brightness-brightness(x,y-1)) > threshold:
            red = 0
            green = 0
            blue = 0
        else:
            red = 255
            green = 255
            blue = 255
            
        det_im = create_color(red,green,blue)
        set_color(new_image,x,y,det_im)
        
    return new_image

I am writing this code to check the individual pixels of every point within an image and I need to compare the brightness of that point with the brightness of the point directly underneath it. Would this be the best way to go about solving this? I am also getting a 'float' object is not callable error with this code.

Mubashar Javed
  • 1,197
  • 1
  • 9
  • 17
beyati
  • 1
  • 2
    Brightness appears to be both a variable and a function? – Stefan Feb 02 '21 at 06:18
  • 1
    IMHO, iterating over images with Python `for` loops is practically never a good method - see if you can use (vectorised) Numpy or (SIMD) OpenCV instead - it will be 10-100x faster. Using a 3-channel RGB image to store your True/False output is also wasteful of memory. It is generally appreciated if you provide complete code, including `import` statements and input and expected output images. Thank you. – Mark Setchell Feb 02 '21 at 09:37
  • create_color and set_color are missing , cant run your code – pippo1980 Feb 03 '21 at 09:58
  • new_image = copy(image) ?? should I read new_image = image.copy() ? or new_image = copy.copy(image) – pippo1980 Feb 03 '21 at 10:07
  • using pillow (PIL) I get for x,y, (r,g,b) in image: TypeError: 'Image' object is not iterable – pippo1980 Feb 03 '21 at 10:08
  • try r, g, b = image.getpixel((x, y)) if you are using pillow see : https://stackoverflow.com/questions/11064786/get-pixels-rgb-using-pil – pippo1980 Feb 03 '21 at 10:37
  • or pixels = image1.load() # create the pixel map for x in range(image1.size[0]): for y in range(pixels[1]): print('XY : ', x,y,' values : ',pixels[x,y]) see here: https://stackoverflow.com/questions/36468530/changing-pixel-color-value-in-pil – pippo1980 Feb 03 '21 at 11:10

1 Answers1

0

using this image as input

input

and this code:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Feb  3 10:41:37 2021

@author: Pietro

"""

import numpy as np

from PIL import Image

# import copy


image1 = Image.open('imgQ.tif', mode='r')

print('image size : ', image1.size)

print('image mode : ', image1.mode)

image2 = image1.convert('RGB')


print('image size : ', image2.size)

print('image mode : ', image2.mode)



def detect_edges (image: Image, threshold: float) -> Image: 
    
    
      image2_copy = image.copy()
      image2_copy.show()
      x, y = image2_copy.size
    

      
      arrayz = (np.random.randint(0,1 ,(x , y))).astype(np.float)
      print('arrayz shape : ',arrayz.shape)
      print('arrayz dim :',arrayz.ndim,   arrayz[0,:].shape, arrayz[:,0].shape)
      print('arrayz type :',arrayz.dtype)
      # for i in arrayz:
      #      print(i)
      # print(arrayz)
      pixels = image2_copy.load() # create the pixel map    
      for x in range(image2_copy.size[0]):
           for y in range(image2_copy.size[1]):
               r,g,b = pixels[x,y] 
               brightness = (r+b+g)/3
               arrayz[x,y]=brightness
      print(arrayz)
      for x in range(image2_copy.size[0]):
            for y in range(image2_copy.size[1]): 
                if abs(arrayz[x,y]-arrayz[x-1,y-1]) > threshold:
                       print('********************')
                       pixels[x,y] = (0,0,0)
            else:
                 pixels[x,y] = (255,255,255)
                 
      print(pixels)
      image2_copy.show()
      image2_copy.save('imgQ_copy.tif')
                                                                  
    
     # return new_image

imageM = detect_edges(image2, 100)

# imageM.show()

in python3 I get as result tis image:

output

Not sure if thats what you were talking about,

and I belive there is something wrong in the approach used, if you zoom at the edges of the polygons the black edge is not present for some of the pixel, I think this is about the algorithm you were thinking, but could be due to my code as well, I am not versed on Images, hope someone better could shed some light on your question

pippo1980
  • 2,181
  • 3
  • 14
  • 30