Check if image background is simple or complicated python3 - image-processing

I have set of real pictures. Is there a way to check if the background is a simple color or a complex/ complicated background using python3?
2 types of images:
Simple background as human see it as 1 color but because of the lighting, each pixel values are different:
Complicated/ Complex background
I tried many methods but none of them works:
colors counter. Because of lightings and shades, each pixel color values are different.
pixel variance.
colors bin
color quantize

Related

Is it possible to use imagemagick-convert's histogram visualisation option without counting transparent pixels?

Apologies if this is a duplicate. I've been Googling this for a day.
Goal
I have a cartoon-like image of a character on a transparent background (mix of transparent black and white pixels). (Think something like a Pikachu or a logo - a solid, non-square shape on a blank background.) The only data I'm interested in analysing are the colour distributions of the pixels within this shape. I want to do this for multiple reasons, including assessing the palette of the character. I would prefer to use ImageMagick's generated histograms alongside it right now, rather than manipulating the text output.
Issue
Using convert image.png -alpha off histogram:histogram.gif or similar results in a histogram where the RGB channels are very short due to huge spikes on the left and right. Example ImageMagick can, say, replace the transparent pixels with a given opaque colour, but that predictably replicates the issue in another channel. This is the result of filling the transparent pixels with #008000. It seems to me this is because the image is at least 50% black/white pixels with opacity 0, way more than any other single colour.
Alternatives Tried
Quantising does not produce a remotely desired result, because the averaged colours are so much blander than the ones used in the picture.
I know I can take the metadata output of the histogram and exclude #FFFFFF, #000000, and so on.
However, I would ideally be able to use ImageMagick's inbuilt visualisation simply because it would make my life a lot easier at this stage and I would not have to build my own visualisations. So what I want is the visualisation without it having counted transparent (or specified colour) pixels towards the number of pixels in the image.
Is this possible within ImageMagick? If not, I'll just manipulate the text histogram.

ML noise filtering

Given an image (2048x2018) with a lot of noise of random pixels (>90% for each possible image), there are several pixels of the same color distributed across entire image. I want to remove noise and leave only pixels of the same color. Let's say from all random pixels 10 red, 15 orange and 14 black pixels should be left.
However, colors of repeating pixels are not known beforehand. There are multiple images with different colors. So I'm trying to write an algorithm that would filter an image until only repeating colors are left. Number of repeating colors should be as close as possible to actual number, but not necessarily exact.
I think this can be achieved by building a network to filter the noise out. But the data seem to be too random to use conventional networks since there are no patterns except of repeating pixels. Is there a way to calcify distributed values without any real pattern? Maybe there is any another way to do it without brute force sorting of each color?
Sounds like you need to use some sort of pooling, specifically Average Pooling. You could use pooling to first find out what colors are dominant in the image, and then remove pixels of all colors other than the dominant colors to leave only the colors that you want.

Most prevalent color on a background by changing color space

I have a sheet of paper on another surface. I want to figure out when the paper ends and the surface begins.
I'm using this approach:
1. Convert image to pixel array
2. Pick 3 random 20x20 squares and frequency count the colors
3. The highest frequency is the background
However, the problem is that I get over 100 colors every time I do this on an actual image taken by the camera.
I think I can fix it by putting the image in 16 colors palette. Is there a way to do this change on a UIImage or CGImage?
Thanks
Your colours are probably very close together. How about calculating the distance (the cumulative absolute difference between red, green and blue values) from each sampled colour to a reference colour - just use the first one you sample as reference. If the distance is large, you have a different colour. If the distance is small, you have the same colour with minor variations in lighting or other camera artefacts.
Basically this is applying a filter in a very simple manner. It is up to you to decide how big the difference has to be for the colours to be considered different, but you could decide that by looking at the median difference of all the colours and grouping them into over/under samples.
You might also get good results from applying a Core Image filter to the sample images, such as CIColorClamp (CISpotColor looks better but is OS X only). if you can find a suitable filter there is a good chance it will be simpler and faster than doing it yourself.

Background Color for Positive Samples in Haar cascade training in OpenCV

I noticed, that in OpenCV's opencv_createsamples utility for Haarcascade training you can specify a background color, which is black by default. Since I am now creating the positive samples I was wondering if it was beneficial to set all background pixels in the positive images to black, or if this would actually hurt the recognition / training process. Any advice?
For future reference: Results are a lot better when the background is not entirely black. So using croped images without presegementation works well.
The bgcolor parameter is 0 by default which does mean black but you can specify your own 8 bit color value. eg: To specify white, use -bgcolor 255
If you have the same background color in your positives and you supply the background images, then the positive will be overlaid on to the background images with the positives own background suitably turned transparent.
You can observe this in effect using the -show parameter

uneven illuminated images

How to get rid of uneven illumination from images, that contain text data, usually printed but may be handwritten? It can have some spots of lights because the light reflected while making picture.
I've seen the Halcon program's segment_characters function that is doing this work perfectly,
but it is not open source.
I wish to convert an image to the image that has a constant illumination at background and more dark colored regions of text. So that binarization will be easy and without noise.
The text is assumed to be dark colored than it's background.
Any ideas?
Strictly speaking, assuming you have access to the image's pixels (you can search online for how to accomplish this in your programming language as the topic is abundantly available), the exercise involves going over the pixels once to determine a "darkness threshold". In order to do this you convert each pixel from RGB to HSL in order to get the lightness level component for each pixel. During this process you calculate an average lightness for the whole image which you can use as your "darkness threshold"
Once you have the image average lightness level, you can go over the image pixels once more and if a pixel is less than the darkness threshold, set it's color to full white RGB(255,255,255), otherwise, set it's color to full black RGB (0,0,0). This will give you a binary image with in which the text should be black - the rest should be white.
Of course, the key is in finding the appropriate darkness threshold - so if the average method doesn't give you good results you may have to come up with a different method to augment that step. Such a method could involve separating the image in the primary channels Red, Green, Blue and computing the darkness threshold for each channel separately and then using the aggressive threshold of the three..
And lastly, a better approach may be to compute the light levels distribution - as opposed to simply the average - and then from that, the range around the maximum is what you want to keep. Again, go over each pixel and if it's lightness fits the band make it black, otherwise, make it white.
EDIT
For further reading about HSL I recommend starting with the Wiky entry on HSL and HSV Color spaces.
Have you tried using morphological techniques? Closure-by-reconstruction (as presented in Gonzalez, Woods and Eddins) can be used to create a grayscale representation of background illumination levels. You can more-or-less standardize the effective illumination by:
1) Calculating the mean intensity of all the pixels in the image
2) Using closure-by-reconstruction to estimate background illumination levels
3) Subtract the output of (2) from the original image
4) Adding the mean intensity from (1) to every pixel in the output of (3).
Basically what closure-by-reconstruction does is remove all image features that are smaller than a certain size, erasing the "foreground" (the text you want to capture) and leaving only the "background" (illumination levels) behind. Subtracting the result from the original image leaves behind only small-scale deviations (the text). Adding the original average intensity to those deviations is simply to make the text readable, so that the resulting picture looks like a light-normalized version of the original image.
Use Local-Thresholding instead of the global thresholding algorithm.
Divide your image(grayscale) in to a grid of smaller images (say 50x50 px) and apply the thresholding algorithm on each individual image.
If the background features are generally larger than the letters, you can try to estimate and subsequently remove the background.
There are many ways to do that, a very simple one would be to run a median filter on your image. You want the filter window to be large enough that text inside the window rarely makes up more than a third of the pixels, but small enough that there are several windows that fit into the bright spots. This filter should result in an image without text, but with background only. Subtract that from the original, and you should have an image that can be segmented with a global threshold.
Note that if the bright spots are much smaller than the text, you do the inverse: choose the filter window such that it removes the light only.
The first thing you need to try and do it change the lighting, use a dome light or some other light that will give you a more diffuse and even light.
If that's not possible, you can try some of the ideas in this question or this one. You want to implement some type of "adaptive threshold", this will apply a local threshold to individual parts of the image so that the change in contrast won't be as noticable.
There is also a simple but effective method explained here. The simple outline of the alrithm is the following:
Split the image up into NxN regions or neighbourhoods
Calculate the mean or median pixel value for the neighbourhood
Threshold the region based on the value calculated in 2) or the value from 2) minus C (where C is a chosen constant)
It seems like what you're trying to do is improve local contrast while attenuating larger scale lighting variations. I'll agree with other posters that optimizing the image through better lighting should always be the first move.
After that, here are two tricks.
1) Use smooth_image() operator to convolve a gaussian on your original image. Use a relaitively large kernel, like 20-50px. Then subtract this blurred image from your original image. Apply scale and offset within sub_image() operator, or use equ_histo() to equalize histogram.
This basically subtracts the low spatial frequency information from the original, leaving the higher frequency information intact.
2) You could try highpass_image() operator, or one of the laplacian operators to extract a gradiant image.

Resources