Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Now I agree that you should avoid writing loops in Python, but if I understand your question correctly, you want to have a loop over a precomputed set of array coordinates and set the matching image pixels to 1. Here is a working example using Python which displays using OpenCV. Hope it helps.

import matplotlib.pyplot as plt
import numpy as np
import cv2

def plot_dots(coords,img_h,img_w):
    img = np.zeros((img_h,img_w), dtype=np.uint8)
    for c in range(coords.shape[1]):
        img[coords[0,c],coords[1,c]] = 1
    return img

if __name__ == "__main__":
    numdots = 100
    img_w = 800
    img_h = 600
    xcoords = np.random.randint(0,img_w-1,(numdots,))
    ycoords = np.random.randint(0,img_h-1,(numdots,))
    coords = np.vstack((ycoords,xcoords))
    img = 255 * plot_dots(coords,img_h,img_w)
    cimg = np.dstack((img,img,img))
    dimg = cimg.astype(np.uint8)
    while True:
        cv2.imshow('dots', dimg)
        ch = cv2.waitKey(1)
        if (ch > 0):
            break
    cv2.destroyAllWindows()