Posts: 6
Joined: Wed Jan 10, 2018 11:15 pm

Counting pixel colors from an image

Mon Mar 19, 2018 3:37 am


I want to make a program that tells what colors (pixels) that image has the most. I will use this result to change the LED color. I am using neoPixels from Adafruit and connect through 74AHCT125 - Quad Level-Shifter (3V to 5V).

I know that I have to get a picture from file and go line by line: x and y. But what is the best library I should use for this task. Today, I have spent like 6 hours just to capture an image and display it because I don't understand yet how python works. And, basically, there are so many possible libraries, python2/ 3, and syntaxes that I have to consider. These are different from what basic programming level I have from other language.

Anyway, This is currently my code now at the part of capture and show an image:

My project info : Rpi 3 model B. camera hardware is picam v2.1 8mega pixels
I display graphic using imagemagick. I have code in python 3 so far.

Code: Select all

from time import sleep
from picamera import PiCamera
from PIL import Image

camera = PiCamera()
#camera.resolution = (1024, 768)
camera.resolution = (1280, 768)
# Camera warm-up time
image = Image.open('camImages/foo.jpg')
I found this tutorial http://www.cl.cam.ac.uk/~db434/raspi/blob_detection/
But they use camera that is not a pi cam and I have tried this code but could not figure it out how to change it. The program did not get the right image path. They use imageProcessing library.

Code: Select all

# Create a camera 
cam = Camera(160, 120) 

# use the camera's width and height to set the viewer size 
view = Viewer(cam.width, cam.height, "Blob finding") 
So my main questions are:
1. Is imageProcessing the right one I should go for?? Because I have downloaded it but this doesn't work. If it is the right one, than I will dig deeper what's wrong with it. If no, what should I do?
2. Are there any similar pixel searching code/tutorial in python I can learn from? If you know any please provide the link for me.
3. I have download the improc module but the Rpi doesn't see it. How to fix this? Here is what it says:

Code: Select all

Traceback (most recent call last):
  File "/home/pi/Desktop/Tuang/viewImage.py", line 2, in <module>
    from imgproc import *
ImportError: No module named 'imgproc'
I've learn from this link:
That the improc meant to be for python 2.7. But the post owner doesn't say how he will do with it, he just says this did the trick!
You may wonder why then can I move to 2.7, which I wish I could. But I have coded everything in py3 . However, I am willing to change thing but I do not know what is the different that I have to make to fit into py2 and if I moved there just for this library but it is not worth it than I will be more struggle.

Posts: 452
Joined: Thu Oct 23, 2014 12:14 am
Location: Texas
Contact: Website

Re: Counting pixel colors from an image

Tue Mar 20, 2018 3:16 am

Do you want to find the most popular pixel color or do you want the average color of your image?

I'm not a python programmer but I can tell you how I would go about it and maybe then you can program it.

1) Create an array of integers for the color values of each pixel.
2) Create a hashmap that uses the color as a key and the count of pixels that color as the value
3) Find the hashmap entry with the largest value and the key will be the most popular color

I think hashmaps in python are called dictionaries but I wouldn't trust my python knowledge.

Finding the most popular color in an image was an interesting challenge so I wrote a Java program to find the most popular color and some other information about an image.

Code: Select all

import java.awt.image.*;
import java.io.*;
import java.util.*;
import java.util.function.*;
import java.util.stream.*;
import static java.util.stream.Collectors.*;
import javax.imageio.*;

public class CountColors {
    public static void main(String... args) throws Exception {
        // read an image file
        BufferedImage img = ImageIO.read(new File("colors.png"));
        // get the integer values of all the pixels
        int[] pix = img.getRGB(0,0,img.getWidth(),img.getHeight(),
         new int[img.getWidth()*img.getHeight()],0,img.getWidth());

        System.out.println("Width: " + img.getWidth());
        System.out.println("Height: " + img.getHeight());

        System.out.println("Pixels: " + img.getWidth() * img.getHeight());

        // create a Map with color (stored as a hex string) as the key and
        //  the count as the value
        Map<String,Long> map = IntStream.of(pix).
         collect(groupingBy(i -> String.format("%08X",i),counting()));

        System.out.println("Different colors: " + map.size());

        System.out.println("Most popular color: " + map.entrySet().stream().
         max((e1,e2) -> Long.compare(e1.getValue(),e2.getValue())).get());

        System.out.println("Sorted by color:");
         sorted((e1,e2) -> e1.getKey().compareTo(e2.getKey())).

        System.out.println("Sorted by quantity of color:");
         sorted((e1,e2) -> Long.compare(e1.getValue(),e2.getValue())).
The colors are hex ARGB (alpha, red, green blue). Alpha of FF is opaque.

C:\Users\Knute Johnson>java CountColors
Width: 800
Height: 600
Pixels: 480000
Different colors: 5
Most popular color: FF00FF00=138749
Sorted by color:
Sorted by quantity of color:
colors.png (5.63 KiB) Viewed 679 times

Posts: 6
Joined: Wed Jan 10, 2018 11:15 pm

Re: Counting pixel colors from an image

Tue Mar 20, 2018 7:22 pm

Hey Thanks! That really makes scene. I code thing in Java / processing before. So now, I think I have to convert thing to python.

Return to “Beginners”