0

Ok, I got a newbie question in python. So I've a list with the coordinate of some pixels I'm interested for. I'd like to move those pixels into another image or saving those pixels in a new image. I'm working with cv2 and matplotlib so far, I even thought of saving the coordinates with RGB values and writing it onto another image, but I don't got any idea to start with. Sorry if its a kind of dumb question, thanks

  • How about creating a new empty image and do `new_img[x,y, :] = ori_img[x, y, :]` for (x, y) in your list? – lincr Jun 18 '20 at 01:07
  • may you do a further explanation? i tried it, and i just got an syntax error, let's say that my list it's like this img = [[x1, y1], [x2, y2]] and keeps going, idk how to iterate over it so i could replace the values from the original img into the new one – Camilo Segura Jun 18 '20 at 03:10

2 Answers2

0

You can do this just like in numpy array. For example, you want to take first 10x10 part of the RGB image with all three channels. Basically;

new_image = np.zeros([10, 10, 3], dtype=np.uint8)
new_image[:, :, :] = old_image[:10, :10, :]
Can
  • 145
  • 1
  • 11
  • i mean, i just got the coordinates of the pixels which i'm interested in, so this could work if i got the RGB values of the pixels? sorry if my understanding its bad, english isn't my native language – Camilo Segura Jun 18 '20 at 03:06
  • You mean finding the coordinate of the specific pixel value? Of course you can use this to check pixel value in order to find the coordinate you want. However, using numpy function to get index of the specific value is a better way(np.where). Please check: https://stackoverflow.com/questions/27175400/how-to-find-the-index-of-a-value-in-2d-array-in-python – Can Jun 18 '20 at 09:44
0
img_src = cv2.imread('someimg.jpg')
h, w, c = img_src.shape

# an empty black image for saving pixels
img_dest = np.zeros((h, w, c), dtype=np.uint8)


for ixy in xy_list:
    x, y = xy_list[ixy]

    # copy pixels at given locations
    img_dest[x, y, :] = img_src[x, y, :]


cv2.imwrite('output.jpg', img_dest)

Could this solve your problem?

lincr
  • 1,633
  • 1
  • 15
  • 36