Various functions for RGB cube processing (including filtering and density computation). More...
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <time.h>
#include "mdmath.h"
#include "miscIPOL.h"
#include "colorfilteringIPOL.h"
Go to the source code of this file.
Classes | |
struct | infoPixel |
Data structure containing RGB information for each image pixel. More... | |
struct | RGBpoint |
Data structure containg information of a color point in RGB space. More... | |
struct | RGBbin |
List of data structures of type RGBpoint. More... | |
struct | infoProjection |
Information about the projection of an RGB color point onto a line or plane. More... | |
Functions | |
int | initialize_colorfiltering_data (unsigned char *R, unsigned char *G, unsigned char *B, int w, int h, struct RGBbin ****checkOut, struct infoPixel **infopixOut, struct infoPixel **infopixBOut, int **repeatedRGBOut, unsigned char ignore000) |
initialization of data structures for RGB cube processing | |
void | delete_colorfiltering_data (struct RGBbin ***check, struct infoPixel *infopix, struct infoPixel *infopixB) |
free memory used to store temporary data structures | |
void | add_neighboursRGB (float R0, float G0, float B0, struct RGBbin ***check, int iR, int iG, int iB, int rneigh, struct RGBpoint **firstneighIn, struct RGBpoint **lastneighIn, int &nneigh, int &nneighdiff) |
add RGB points to a list of neighbors of a given RGB point in a color image | |
struct RGBpoint * | find_neighboursRGB (float R0, float G0, float B0, struct RGBbin ***check, int rneigh, int nneighmax, int &nneigh, int &nneighdiff) |
create a list of neighbors of a given RGB point (RGB points at L1-distance smaller than a threshold) in a color image | |
void | project_point (float R0, float G0, float B0, float &newR, float &newG, float &newB, float Rcm, float Gcm, float Bcm, float vx, float vy, float vz, unsigned char option) |
project RGB point onto a line or a plane | |
unsigned char | get_projectionRGB (float R0, float G0, float B0, struct RGBpoint *firstneigh, struct infoProjection *infoproj, unsigned char option) |
get parameters of a local line or a plane in the neighborhood of a given RGB point (use PCA) and project the point onto the line (plane) | |
unsigned char | find_projectionRGB (float R0, float G0, float B0, struct RGBbin ***check, struct infoProjection *infoproj, int rneigh, int nneighmin, unsigned char option) |
find the projection of a given RGB point, from a color image, onto a locally defined line or plane | |
void | process_RGBpoint (float R0, float G0, float B0, unsigned char validprojection, struct RGBbin ***check, struct infoPixel *infopix, struct infoPixel *infopixB, struct infoProjection *infoproj, int rsim) |
update RGB values from the projection of initial RGB points onto local lines or planes | |
void | reset_color_info (struct RGBbin ***check) |
reset data structures for a new iteration of the filtering algorithm | |
void | compute_errors_it (struct infoPixel *infopix, struct infoPixel *infopixPrev, int w, int h, float &dprev, int *repeatedRGB) |
compute average difference between RGB values in current and previous iterations of the algorithm | |
void | update_color_info (struct RGBbin ***check, struct infoPixel *infopix, int w, int h) |
update color information for a new iteration of the filtering algorithm | |
void | get_output_image (unsigned char *outR, unsigned char *outG, unsigned char *outB, int w, int h, struct infoPixel *infopix) |
compute output image from updated RGB values | |
int | colorfiltering (unsigned char *R, unsigned char *G, unsigned char *B, unsigned char *outR, unsigned char *outG, unsigned char *outB, int w, int h, struct paramColorFilter *param) |
Main function implementing the color filtering algorithm (LLP algorithm). | |
struct paramColorFilter * | new_colorfiltering_parameters (int rneigh, int rsim, int nneighmin, float eitmax, unsigned char option, unsigned char ignore000) |
Organize input parameters in a data structure. | |
void | delete_colorfiltering_parameters (struct paramColorFilter *param) |
Delete input parameters data structure. | |
float *** | RGBdensities (unsigned char *R, unsigned char *G, unsigned char *B, int w, int h, int rdst, float hdst, float &maxdst) |
Compute the density of the colors of an image. | |
void | delete_RGBdensities (float ***dsts) |
Delete data structure containing density values. | |
void | removeisolatedRGB (unsigned char *R, unsigned char *G, unsigned char *B, unsigned char *outR, unsigned char *outG, unsigned char *outB, int w, int h, int r, int nmin, int &nRGB, int &nisolatedRGB, int &nisolatedpixels) |
Filter out "isolated" colors in RGB space, that is, colors with a small number of neighbors. |
Various functions for RGB cube processing (including filtering and density computation).
Definition in file colorfilteringIPOL.cpp.
void add_neighboursRGB | ( | float | R0, | |
float | G0, | |||
float | B0, | |||
struct RGBbin *** | check, | |||
int | iR, | |||
int | iG, | |||
int | iB, | |||
int | rneigh, | |||
struct RGBpoint ** | firstneighIn, | |||
struct RGBpoint ** | lastneighIn, | |||
int & | nneigh, | |||
int & | nneighdiff | |||
) |
add RGB points to a list of neighbors of a given RGB point in a color image
Adds to the list all the colors of the image whose RGB coordinates are in the range [iR, iR+1), [iG, iG+1), [iB, iB+1)
[in] | R0,G0,B0 | initial RGB point |
[in] | check | check[iR][iG][iB] is a pointer to a RGBbin data structure that contains the list of pixels whose color is in the range [iR, iR+1), [iG, iG+1), [iB, iB+1) |
[in] | rneigh | threshold distance that defines the neighborhood around the initial RGB point |
in/out] | firstneighIn, lastneighIn pointers to the beginning and end of the list | |
[out] | nneigh | number of color points in the list (each point appears as many times as pixels in the image have this color) |
[out] | nneighdiff | number of diferent colors in the list (without taking into account the number of times it is repeated in the image) |
Definition at line 304 of file colorfilteringIPOL.cpp.
int colorfiltering | ( | unsigned char * | R, | |
unsigned char * | G, | |||
unsigned char * | B, | |||
unsigned char * | outR, | |||
unsigned char * | outG, | |||
unsigned char * | outB, | |||
int | w, | |||
int | h, | |||
struct paramColorFilter * | param | |||
) |
Main function implementing the color filtering algorithm (LLP algorithm).
Algorithm description (LLP algorithm)
Image colors are considered as points in a 3D space, namely the color cube. The algorithm replaces each RGB color in the cube by its projection onto a line (or plane) locally defined. This line (or plane) is the one that better fits the spatial distribution of the colors in a neighborhood of the considered RGB point, and it is computed by Principal Components Analysis (PCA). Only points with a large enough number of neighboring colors are projected. The process is iterated until the average error between corresponding colors in consecutive iterations is below a threshold.
[in] | R,G,B | pointers to red, green and blue channels of the input image |
[out] | outR,outG,outB | pointers to red, green and blue channels of the output image |
[in] | w,h | width and height of the input (and output) images |
[in] | param | data structure with the parameters of the algorithm: |
param.rneigh | radius of local neighborhood for computation of lines or planes | |
param.nneighmin | minimum number of neighbors (number of different colors) needed to compute lines or planes | |
param.rsim | speed up factor, the same line or plane computed for a given RGB point is used for its neighbors at L1-distance rsim | |
param.eitmax | stop criterion of the algorithm. The algorithm is iterated until the average difference of corresponding colors in two consecutive iterations is below eitmax | |
param.option | 1 project to line, 2 project to plane | |
param.ignore000 | if TRUE ignore (0,0,0) color points (eventually belonging to pixels discarded in a pre-processing step) |
Definition at line 1017 of file colorfilteringIPOL.cpp.
void compute_errors_it | ( | struct infoPixel * | infopix, | |
struct infoPixel * | infopixPrev, | |||
int | w, | |||
int | h, | |||
float & | dprev, | |||
int * | repeatedRGB | |||
) |
compute average difference between RGB values in current and previous iterations of the algorithm
[in] | infopix,infoPrev | information about the colors of the pixel in the current and previous iterations of the algorithm |
[in] | w,h | dimensions of the input image (w*h = number of pixels) |
[out] | dprev | average difference between current and previous iterations of the algorithm [in] repeatedRGB array of labels for the image pixels. Only one instance of each color has a label different from zero |
This function is used in the main loop to decide whether further iterations are needed (if dprev is above a certain threshold)
Definition at line 845 of file colorfilteringIPOL.cpp.
void delete_colorfiltering_data | ( | struct RGBbin *** | check, | |
struct infoPixel * | infopix, | |||
struct infoPixel * | infopixB | |||
) |
free memory used to store temporary data structures
Definition at line 256 of file colorfilteringIPOL.cpp.
void delete_colorfiltering_parameters | ( | struct paramColorFilter * | param | ) |
Delete input parameters data structure.
Definition at line 1151 of file colorfilteringIPOL.cpp.
void delete_RGBdensities | ( | float *** | dsts | ) |
Delete data structure containing density values.
Definition at line 1247 of file colorfilteringIPOL.cpp.
struct RGBpoint* find_neighboursRGB | ( | float | R0, | |
float | G0, | |||
float | B0, | |||
struct RGBbin *** | check, | |||
int | rneigh, | |||
int | nneighmax, | |||
int & | nneigh, | |||
int & | nneighdiff | |||
) | [read] |
create a list of neighbors of a given RGB point (RGB points at L1-distance smaller than a threshold) in a color image
[in] | R0,G0,B0 | initial RGB point |
[in] | check | check[iR][iG][iB] is a pointer to a RGBbin data structure that contains the list of pixels whose color is in the range [iR, iR+1), [iG, iG+1), [iB, iB+1) |
[in] | rneigh | threshold distance that defines the neighborhood around the initial RGB point |
[in] | nneighmax | if nneighmax > 0, maximum number of different RGB points in the list, otherwise do not limit the size of the list |
[out] | nneigh | number of color points in the list (each point appears as many times as pixels in the image have this color) |
[out] | nneighdiff | number of diferent colors in the list (without taking into account the number of times it is repeated in the image) |
The list is filled by looking for neighbors at L1-distances 0, 1, 2,..., rneigh, until at least nneighmax different colors are found (in the case that nneighmax > 0), or until the maximum distance rneigh is reached (in the case that nneighmax <= 0)
Definition at line 376 of file colorfilteringIPOL.cpp.
unsigned char find_projectionRGB | ( | float | R0, | |
float | G0, | |||
float | B0, | |||
struct RGBbin *** | check, | |||
struct infoProjection * | infoproj, | |||
int | rneigh, | |||
int | nneighmin, | |||
unsigned char | option | |||
) |
find the projection of a given RGB point, from a color image, onto a locally defined line or plane
The list of neighbors (RGB points at L1-distance below some threshold) of the considered point is first computed. Each RGB point appears in the list as many times as it is found in the image (=number of pixels with the same RGB value), however the number of different RGB points is also computed. If the number of different RGB points is large enough then Principal Components Analysis is used to compute the line or plane that better fits the spatial distribution of RGB points. Finally the original point is projected onto this line or plane.
[in] | R0,G0,B0 | initial RGB point |
[in] | check | check[iR][iG][iB] is a pointer to a RGBbin data structure that contains the list of pixels whose color is in the range [iR, iR+1), [iG, iG+1), [iB, iB+1) |
[out] | infoproj | data structure containing the parameters of the line or plane and the projection of the initial point |
[in] | rneigh | threshold distance that defines the neighborhood of the initial RGB point |
[in] | nneighmin | minimum number of different RGB points needed to perform PCA and compute projection |
[in] | option | if option=2 compute plane, otherwise compute line |
Definition at line 674 of file colorfilteringIPOL.cpp.
void get_output_image | ( | unsigned char * | outR, | |
unsigned char * | outG, | |||
unsigned char * | outB, | |||
int | w, | |||
int | h, | |||
struct infoPixel * | infopix | |||
) |
compute output image from updated RGB values
[in] | outR,outG,outB | color channels of the output image |
[in] | w,h | dimensions of the output image |
[in] | infopix | information about the colors of the pixel |
Definition at line 939 of file colorfilteringIPOL.cpp.
unsigned char get_projectionRGB | ( | float | R0, | |
float | G0, | |||
float | B0, | |||
struct RGBpoint * | firstneigh, | |||
struct infoProjection * | infoproj, | |||
unsigned char | option | |||
) |
get parameters of a local line or a plane in the neighborhood of a given RGB point (use PCA) and project the point onto the line (plane)
The spatial distribution of the RGB points in the neighborhood of the considered point (L1-distance below a given threshold), including the point itself, is analyzed using Principal Components Analysis. For this analysis, the frequency of the color point (i.e. the number of times it appears in the image) is taken into account
The output line (or plane) is defined by the first (or two first) principal components and passes through the center of mass of the considered RGB points
[in] | R0,G0,B0 | initial RGB point |
[in] | firstneigh | pointer to the list of neighbors of the input point |
[out] | infoproj | data structure containing the parameters of the line or plane and the projection of the initial point |
[in] | if | option=2 compute plane, otherwise compute line |
Definition at line 550 of file colorfilteringIPOL.cpp.
int initialize_colorfiltering_data | ( | unsigned char * | R, | |
unsigned char * | G, | |||
unsigned char * | B, | |||
int | w, | |||
int | h, | |||
struct RGBbin **** | checkOut, | |||
struct infoPixel ** | infopixOut, | |||
struct infoPixel ** | infopixBOut, | |||
int ** | repeatedRGBOut, | |||
unsigned char | ignore000 | |||
) |
initialization of data structures for RGB cube processing
[in] | R,G,B | pointers to red, green and blue channels of the input image |
[out] | checkOut | checkOut[iR][iG][iB] is a pointer to a RGBbin data structure that contains the list of pixels whose color is in the range [iR, iR+1), [iG, iG+1), [iB, iB+1) |
[out] | infopixOut | infopixOut[k] is a pointer to a infoPixel data structure that contains information about the color of pixel k |
[out] | infopixOutB | infopixOuBt[k] is a pointer to a infoPixel data structure that contains information about the color of pixel k |
[out] | repeatedRGBOut | repeatedRGBOut[k] counts the number of occurrences of the color of ṕxel k, provided that pixel k is the first occurrence of the color, else repeatedRGBOut[k]=0 |
[in] | ignore000 | if ignore000=1 do not take into account pixels with (0, 0, 0) color (eventually excluded from processing after a preprocessing step) |
Remark: Initially infopixOut and infopixOutB contain the same information, but as the algorithm iterates one of them is used to store the update information while the other keeps the old RGB values. Their role switches as the algorithm iterates.
Definition at line 161 of file colorfilteringIPOL.cpp.
struct paramColorFilter* new_colorfiltering_parameters | ( | int | rneigh, | |
int | rsim, | |||
int | nneighmin, | |||
float | eitmax, | |||
unsigned char | option, | |||
unsigned char | ignore000 | |||
) | [read] |
Organize input parameters in a data structure.
[in] | rneigh | distance defining the neighborhood of a color point (neighbors are RGB points at L1-distance below rneigh) |
[in] | rsim | distance defining a small neighborhood around each color point, it is used to speed up the algorithm. It must be much smaller than rneigh, typically rsim=rneigh/3 |
[in] | nneighmin | minimum number of neighbors needed to compute a local line or plane in the vicinity of a color point |
[in] | eitmax | stop criterion of the algorithm. The algorithm is iterated until the average difference of corresponding colors in two consecutive iterations is below eitmax |
[in] | option | project points to lines (option=1) or planes (option=2) |
[in] | ignore000 | if set to 1, do not take into account pixels with (0,0,0) RGB value (eventually discarded for computation in a preprocessing step) |
Definition at line 1129 of file colorfilteringIPOL.cpp.
void process_RGBpoint | ( | float | R0, | |
float | G0, | |||
float | B0, | |||
unsigned char | validprojection, | |||
struct RGBbin *** | check, | |||
struct infoPixel * | infopix, | |||
struct infoPixel * | infopixB, | |||
struct infoProjection * | infoproj, | |||
int | rsim | |||
) |
update RGB values from the projection of initial RGB points onto local lines or planes
[in] | R0,G0,B0 | initial RGB point |
[in] | validprojection | label indicating whether the point is projected to a line (1), a plane (2) or is not projected at all (0) |
[in] | check | check[iR][iG][iB] is a pointer to a RGBbin data structure that contains the list of pixels whose color is in the range [iR, iR+1), [iG, iG+1), [iB, iB+1) |
[in] | infopix | information about the current colors of the pixels |
[out] | infopixB | information about the updated colors of the pixels |
[out] | infoproj | data structure containing the parameters of the line or plane and the projection of the initial point |
[in] | rsim | speed up parameter, all the points in a neighborhood of radius rsim of a the initial color point are projected to the same line/plane |
Definition at line 721 of file colorfilteringIPOL.cpp.
void project_point | ( | float | R0, | |
float | G0, | |||
float | B0, | |||
float & | newR, | |||
float & | newG, | |||
float & | newB, | |||
float | Rcm, | |||
float | Gcm, | |||
float | Bcm, | |||
float | vx, | |||
float | vy, | |||
float | vz, | |||
unsigned char | option | |||
) |
project RGB point onto a line or a plane
[in] | R0,G0,B0 | coordinates of the initial RGB point |
[out] | newR,newG,newB | coordinates of the projected point |
[in] | Rcm,Gcm,Bcm | point coordinates of a point contained in the line or plane |
[in] | vx,vy,vy | coordinates of the unitary direction vector of the line, or the orthonormal vector to the plane |
[in] | option | option=1 project to line, otherwise project to plane |
Definition at line 493 of file colorfilteringIPOL.cpp.
void removeisolatedRGB | ( | unsigned char * | R, | |
unsigned char * | G, | |||
unsigned char * | B, | |||
unsigned char * | outR, | |||
unsigned char * | outG, | |||
unsigned char * | outB, | |||
int | w, | |||
int | h, | |||
int | r, | |||
int | nmin, | |||
int & | nRGB, | |||
int & | nisolatedRGB, | |||
int & | nisolatedpixels | |||
) |
Filter out "isolated" colors in RGB space, that is, colors with a small number of neighbors.
The result is an image identical to the original, except at the pixels with "isolated" colors, whose value has been replace by (0, 0, 0). The number of neighbors of a pixel is the number of color points at L1-distance below some threshold. Each color appears in the list of neighbors as many times as it is present in the image (i.e. the frequency of the color is taken into account in the computations).
[in] | R,G,B | color channels of the input image |
[out] | outR,outG,outB | color channels of the output image |
[in] | w,h | dimensions of input and output images |
[in] | r | radius of the neighborhood around each color point |
[in] | nmin | threshold for the minimum number of neighboring color points needed to decide that a color is not "isolated" |
[out] | nRGB | number of colors in input image |
[out] | nisolatedRGB | number of "isolated" colors |
[out] | nisolatedpixels | number of pixel with "isolated" color |
Definition at line 1284 of file colorfilteringIPOL.cpp.
void reset_color_info | ( | struct RGBbin *** | check | ) |
reset data structures for a new iteration of the filtering algorithm
[in] | check | check[iR][iG][iB] is a pointer to a RGBbin data structure that contains the list of pixels whose color is in the range [iR, iR+1), [iG, iG+1), [iB, iB+1) |
Definition at line 809 of file colorfilteringIPOL.cpp.
float*** RGBdensities | ( | unsigned char * | R, | |
unsigned char * | G, | |||
unsigned char * | B, | |||
int | w, | |||
int | h, | |||
int | rdst, | |||
float | hdst, | |||
float & | maxdst | |||
) |
Compute the density of the colors of an image.
The density of a color C (in RGB space) is defined as
where is the set of neighbors of C is the Euclidean distance between colors C and C' in RGB space and is a weighting parameter
The set of neighbors of a color C is composed of all the colors at L1-distance below some threshold . Each color appears in the set of neighbors as many times as it is present in the image (i.e. the frequency of the color is taken into account in the computations). Tipically the weighting parameter is so the weight of the furthest neighbors is
[in] | R,G,B | color channels of the image |
[in] | w,h | dimensions of the image |
[in] | rdst | radius of the neighbor around each color point |
[in] | hdst | weighting parameter |
[out] | maxdst | maximum density value |
Definition at line 1186 of file colorfilteringIPOL.cpp.
update color information for a new iteration of the filtering algorithm
[in] | check | check[iR][iG][iB] is a pointer to a RGBbin data structure that contains the list of pixels whose color is in the range [iR, iR+1), [iG, iG+1), [iB, iB+1) |
[in] | infopix | information about the colors of the pixel |
[in] | w,h | dimensions of the input image (w*h = number of pixels) |
Definition at line 884 of file colorfilteringIPOL.cpp.