0

How fast change pixels values? In C# what i need to do is only use GetPixel() to get pixel value and SetPixel() to change it (its pretty easy to use but slow, MarshallCopy and Lock/UnlockBits is much faster).

In this code, i marking black pixels as 1 and white pixels as 0

import tkFileDialog
import cv2
import numpy as np
from matplotlib import pyplot as plt

path = tkFileDialog.askopenfilename()
bmp = cv2.imread(path) #reading image
height, width, channels = bmp.shape

if channels == 3:
    bmp = cv2.cvtColor(bmp, cv2.COLOR_BGR2GRAY) #if image have 3 channels, convert to BW
    bmp = bmp.astype('uint8')

bmp = cv2.adaptiveThreshold(bmp,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,\
            cv2.THRESH_BINARY,11,2) #Otsu thresholding

imageData = np.asarray(bmp) #get pixels values

pixelArray = [[0 for y in range(height)] for x in range(width)] #set size of array for pixels

for y in range(len(imageData)):
    for x in range(len(imageData[0])):
        if imageData[y][x] == 0:
            pixelArray[y][x] = 1 #if black pixels = 1
        else:
            pixelArray[y][x] = 0 #if white pixels = 0

In c#, it can looks like this:

for (y = 0; y < bmp.Height-1; y++)
            {
                for (x = 0; x < bmp.Width-1; x++)
                {
                    if (pixelArray[y, x] == 1)
                        newImage.SetPixel(x, y, Color.Black); //printing new bitmap
                    else
                        newImage.SetPixel(x, y, Color.White);
                }
            }
            image2.Source = Bitmap2BitmapImage(newImage);

In the next step i will marking countour pixels as "2", but now i want to ask you, how to set new image in python from my specific value and then, display it? For experimental purpose, i want to invert image (from B&W to W&B) only by byte valuse. Can you help me how to do it?

EDIT1

I think i found a solution, but i have GREYSCALE image with one channel (i think thats how it works when i using cv2.cvtColor to convert 3 channels image to greyscale image). The function like this:

im[np.where((im == [0,0,0]).all(axis = 2))] = [0,33,166]

Could work pretty well, but how to make that function work with greyscale image? I want to set some black pixels (0) into White (255)

9
  • 1
    If black is 0 and white is 1, you can invert with image=1-image. If black is 0 and white is 255, you can invert with image=255-image. Commented Jul 4, 2018 at 7:40
  • Thanks for your response! But i dont want to simply invert image but sett pixels values according to my pixelArray[][], when "1" - black, when "0" - white. I asking about it because in next step i wil get "2" as countour so it will start to be more complicated Commented Jul 4, 2018 at 7:46
  • You might like to consider a LUT... stackoverflow.com/a/50598388/2836621 Commented Jul 4, 2018 at 7:48
  • I just trying to implement KMM skelezation alghoritm, block diagram here imgur.com/a/zNq7D75 Commented Jul 4, 2018 at 7:48
  • I dont really need LUT because i have only two pixels value, White (255) and Black (0). "1s" and "2s" are Black, "0s" are White In the last step i will have (in pixelArray) values like 0, 1 or 2. According to this matrix, i want to set every pixel (pixel[y][x] == pixelArray[y][x]) compared to values in pixelArray (im deleting countur marked as "2", so "2" pixels == (255) Commented Jul 4, 2018 at 7:54

1 Answer 1

3

For a single channel image (gray scale image) use the following:

First create a copy of the gray image:

gray_2 = gray.copy()

Now assign black pixels to be white:

gray_2[np.where(gray == 0)] = 255
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.