Here's the code in Small Basic:
GraphicsWindow.CanResize = 0
GraphicsWindow.Width = 201
GraphicsWindow.Height = 300
X = 0
circumstance = 0
'Initialization
GraphicsWindow.SetPixel(100,0,"Black")
code = "01111111"
'Start
For i=1 To GraphicsWindow.Height
For X=0 To GraphicsWindow.Width
Program.Delay(1)
If X>0 Then
If GraphicsWindow.GetPixel(X-1,i-1) = "Black" Then
circumstance = circumstance + 1
EndIf
EndIf
If GraphicsWindow.GetPixel(X,i-1) = "Black" Then
circumstance = circumstance + 10
EndIf
If X<GraphicsWindow.Width Then
If GraphicsWindow.GetPixel(X+1,i-1) = "Black" Then
circumstance = circumstance + 100
EndIf
EndIf
TextWindow.WriteLine(circumstance)
EndFor
EndFor
What it does is it looks through the Graphics Window pixel by pixel.
The variable "circumstance" is what it found.
If there is a black pixel to the top left of the target pixel, the first digit will be 1.
If there is a black pixel to the top of the target pixel, the second digit will be 1.
If there is a black pixel to the top right of the target pixel, the third digit will be 1.
But no matter what I try circumstance always outputs 000.
GetPixel always worked for me. What sB version are you using?
BTW the graphics window needs to painted by some color, although its white by default, it won't return white, because there is like no color.
GetPixel returns "#rrggbb" not color name.
GraphicsWindow is filled with "#000000" at the beginning. But the color is transparent so you can see the background color.
Related
I have 2 co-located images, both created in a similar way and both have the size of 7,221 x 119 pixels.
I want to write a logic like this:
If the R,G,B values of a certain pixel (called it x) in image 1 = 0,0,0 (black) And the R,G,B values of pixel x in image 2 = 0,0,0 (black) then change the R,G,B values of pixel x in image 1 to 255,255,255 (white), Else no change.
How can I do this in either Matlab or Python?
You should be able to do this in python with the Pillow package. You need to load the two pixels, check if all the color channels are 0 and if so make them 255, then save the image again. In Python 0 is interpreted as False, so not any(vals) will be True when vals includes only zeros.
from PIL import Image
im1 = Image.open("image1.jpg")
im2 = Image.open("image2.jpg")
pixel = (0, 0)
newcolor = (255,)*3
if not any(im1.getpixel(pixel)) and not any(im2.getpixel(pixel)):
im1.putpixel(pixel, newcolor)
im1.save('image1conv.jpg')
Note that not any(im1.getpixel(pixel)) and not any(im2.getpixel(pixel)) could be rewritten as not any(im1.getpixel(pixel) + im2.getpixel(pixel)), but I think the first way has clearer logic.
I am trying to come up with an algorithm to determine the dominant color in an image (either taken from a devices camera or by selecting an existing photo in the photo library). I have written an iOS 8 application in Swift that can grab the RGB value of each pixel in the image, but I don't really know what to do from there.
For pixels that have a distinct dominant color, say RGB(230, 15, 30), it's pretty easy to determine the dominant color. However, I don't really know what to do for pixels that have RGB values where 2 of the 3 values are similar, say RGB(200, 215, 30).
My original thought was to keep 3 counters (one for each color) and add each pixels corresponding RGB values to that counter. At the end I would divide each counter by the total number of pixels and the max of the 3 values would be the dominant color. However, like I mentioned before, when the results are close to each other I can't say that one color necessarily dominates the other.
Just looking for some thoughts and suggestions
I came up with this problem a few weeks ago, and having read many posts talking about it, I found the best method is Hierarchical Quantization presented by this post: http://aishack.in/tutorials/dominant-color/. Also, I have implemented it in python: https://github.com/wenmin-wu/dominant-colors-py . You can install it with pip:pip install dominantcolors and use it as following:
from dominantcolors import get_image_dominant_colors
dominant_colors = get_image_dominant_colors(image_path='/path/to/image_path',num_colors=3)
An idea:
First step is to reduce the number of colors, for example "Color Quantization using K-Means". In the example from the link, the number of colors was reduced to 64 from 96K.
Second step is to calculate the ratio for every color and pick the biggest value.
You can check my hobby project to find the dominant color in a UIImage: https://github.com/ruuki/ColorFinder
What it does basically is creating clusters of colors of the image and returns the most dominant one in a completion block. You can tweak threshold parameters within the source code. Hope it helps.
i had a similar task to do, here is my python code:
import picamera
import picamera.array
import numpy as np
from math import sqrt, atan2, degrees
def get_colour_name(rgb):
rgb = rgb / 255
alpha = (2 * rgb[0] - rgb[1] - rgb [2])/2
beta = sqrt(3)/2*(rgb[1] - rgb[2])
hue = int(degrees(atan2(beta, alpha)))
std = np.std(rgb)
mean = np.mean(rgb)
if hue < 0:
hue = hue + 360
if std < 0.055:
if mean > 0.85:
colour = "white"
elif mean < 0.15:
colour = "black"
else:
colour = "grey"
elif (hue > 50) and (hue <= 160):
colour = "green"
elif (hue > 160) and (hue <= 250):
colour = "blue"
else:
colour = "red"
if DEBUG:
print rgb, hue, std, mean, colour
return str(int(hue)) + ": " + colour
def scan_colour:
with picamera.PiCamera() as camera:
with picamera.array.PiRGBArray(camera) as stream:
camera.start_preview()
camera.resolution = (100, 100)
for foo in camera.capture_continuous(stream, 'rgb', use_video_port=False, resize=None, splitter_port=0, burst=True):
stream.truncate()
stream.seek(0)
RGBavg = stream.array.mean(axis=0).mean(axis=0)
colour = get_colour_name(RGBavg)
print colour
scan_colour()
What i thought is to build the mean Color of all Pixels and to determine the Color out of the hue angle. For getting grayscale answers i wanted to check if the Color is near the middle line of the Color corpus.
I have a picture of a handwritten letter (say the letter, "y"). Keeping only the first of the three color values (since it is a grayscale image), I get a 111x81 matrix which I call aLetter. I can see this image (please ignore the title) using:
colormap gray; image(aLetter,'CDataMapping','scaled')
What I want is to remove the white space around this letter and somehow average the remaining pixels so that I have an 8x8 matrix (let's call it simpleALetter). Now if I use:
colormap gray; image(simpleALetter,'CDataMapping','scaled')
I should see a pixellated version of the letter:
Any advice on how to do this would be greatly appreciated!
You need several steps to achieve what you want (updated in the light of #rwong's observation that I had white and black flipped…):
Find the approximate 'bounding box' of the letter:
make sure that "text" is the highest value in the image
set things that are "not text" to zero - anything below a threshold
sum along row and column, find non-zero pixels
upsample the image in the bounding box to a multiple of 8
downsample to 8x8
Here is how you might do that with your situation
aLetter = max(aLetter(:)) - aLetter; % invert image: now white = close to zero
aLetter = aLetter - min(aLetter(:)); % make the smallest value zero
maxA = max(aLetter(:));
aLetter(aLetter < 0.1 * maxA) = 0; % thresholding; play with this to set "white" to zero
% find the bounding box:
rowsum = sum(aLetter, 1);
colsum = sum(aLetter, 2);
nonzeroH = find(rowsum);
nonzeroV = find(colsum);
smallerLetter = aLetter(nonzeroV(1):nonzeroV(end), nonzeroH(1):nonzeroH(end));
% now we have the box, but it's not 8x8 yet. Resampling:
sz = size(smallerLetter);
% first upsample in both X and Y by a factor 8:
bigLetter = repmat(reshape(smallerLetter, [1 sz(1) 1 sz(2)]), [8 1 8 1]);
% then reshape and sum so you end up with 8x8 in the final matrix:
letter8 = squeeze(sum(sum(reshape(bigLetter, [sz(1) 8 sz(2) 8]), 3), 1));
% finally, flip it back "the right way" black is black and white is white:
letter8 = 255 - (letter8 * 255 / max(letter8(:)));
You can do this with explicit for loops but it would be much slower.
You can also use some of the blockproc functions in Matlab but I am using Freemat tonight and it doesn't have those… Neither does it have any image processing toolbox functions, so this is "hard core".
As for picking a good threshold: if you know that > 90% of your image is "white", you could determine the correct threshold by sorting the pixels and finding the threshold dynamically - as I mentioned in my comment in the code "play with it" until you find something that works in your situation.
I would like to create a code from a given image. For example, this image:
should get the code 111-111-010.
(Suppose it's a png image and all pixels except the banana itself are transparent.)
If all pixels in a particular square are transparent, the value of this square is 0, otherwise it's 1.
So, given an image, I would like to divide it to squares of a given size (e.g. the banana image is 300x300 pixels, and the squares are 100x100), and then to create a code (string) which is built like described above.
The easiest way would probably be by using each_pixel and just check manually if all pixels in a square are transparent. Is there a better method?
I would take your existing image and shrink it to that 3x3 size:
play = image.resize(3, 3, CubicFilter, 0.5)
Then you can create the code by checking each remaining pixel using:
code = ""
(0..2).each do |ix|
(0..2).each do |iy|
code += play.pixel_color(ix,iy).opacity == 65535 ? "0" : "1"
code += iy == 2 ? "-" : "" unless ix == 2 && iy == 2
end
end
I compare opacity to 65535 because when I inspected a pixel I knew was transparent this is what returned:
=> red=65535, green=65535, blue=65535, opacity=65535
I have an image in which the intensity of Red increases from left to right (similar to a spectrum). Is it possible for me to compare the intensity of red at two different points?
I need to the values of the intensities of red at equal distances for a science project. Is there a script that I can use (in Gimp for example) or can anyone tell me how I can create my own using Python or Scilab? Other tools and languages are also welcome but these two are the ones I'm familiar with.
Thanks. Please comment if my question is not clear enough. I'll try to improve it based on your comments.
You image is a matrix where each entry (pixel) is composed by the red, green and blue components. Usually each R, G, B value is in the range 0 to 255, but that can be vary depending on the image format.
To compare the red intensity at two diferent points, extract the the Red component of the pixel at these two positions. You will have two values that you can compare.
If your spectrum is of intensity i.e. from maximum red (r=255) to black (r=0), then the pixel value of the R channel (of RGB) can be used for the relative calculation (ratio or difference).
If the spectrum is of saturation i.e. from red (full saturated) to grey (completely de-saturated) then you can use the saturation value as your measure.
If you give an example image it will help.
I want to make it easier for you: I don't certainly know about GIMP, but I've done this in VB .NET Just some code like the below. If you want to get the percentage, it's just math: as others users told you, you will get three values corresponding to Red, Green and Blue. This values are from 0 to 255, so 255 means 100% and 0 means 0%. Just get a function to obtain the percentage:
Function getPerc(color) 'color can be R, G, B
getPerc = (color * 100) / 255
End Function
There you are. The code to get the colors can be adapted from:
Sub btnGo_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnGo.Click
Me.Cursor = Cursors.WaitCursor
Application.DoEvents()
' Load the image
Dim bm1 As Bitmap = Image.FromFile(txtFile1.Text)
' Make a difference image.
Dim wid As Integer = bm1.Width
Dim hgt As Integer = bm1.Height
' Create some variables
Dim red As Byte, blue As Byte, green As Byte
Dim color As color
'Go pixel by pixel
For x As Integer = 0 To wid - 1
For y As Integer = 0 To hgt - 1
color = bm1.GetPixel(x, y)
red = color.R
blue = color.B
green = color.G
'your code here
Next y
Next x
End Sub
(which is actually adapted by me from http://www.vb-helper.com/howto_net_image_compare.html )