https://doi.org/10.5201/ipol.2011.blm-cdf
Revision 2ac5a1527b132f8930c5a8ec10cd641388a89e9e authored by Software Heritage on 01 January 2011, 00:00:00 UTC, committed by Software Heritage on 22 June 2011, 00:00:00 UTC
0 parent
Tip revision: 2ac5a1527b132f8930c5a8ec10cd641388a89e9e authored by Software Heritage on 01 January 2011, 00:00:00 UTC
ipol: Deposit 654 in collection ipol
ipol: Deposit 654 in collection ipol
Tip revision: 2ac5a15
rgbprocess.cpp
/*
* Copyright (c) 2009-2010 Nicolas Limare <nicolas.limare@cmla.ens-cachan.fr>
* Jose-Luis Lisani <joseluis.lisani@uib.es>
* All rights reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @mainpage Image color cube dimensional filtering and visualization
*
* README.txt:
* @verbinclude README.txt
*/
/**
* @file rgbprocess.cpp
* @brief Main File: manages all the operations needed by the
* blm_color_dimensional_filtering IPOL demo
*
*
*
* @author Jose-Luis Lisani <joseluis.lisani@uib.es>
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "io_png/io_png.h"
#include "rgbprocess_lib.h"
#define VERSION "0.20110518"
/**
* @brief main function call (manages all the processing operations
* needed by the blm_color_dimensional_filtering IPOL demo)
*
* Options of usage:
*
* filter: filters the colors of an image using applies LLP2 algorithm
* Input: input image (PNG format)
* Output: output image (PNG format)
*
* rmisolated: removes isolated color points from an image
* Input: input image (PNG format)
* Output: output image (PNG format)
*
* pcaviews: creates 3 images displaying the principal views of the
* RGB cube of a color image, the principal axis are computed
* by Principal Components Analysis of the distribution of colors
* of a reference image
* Input: input and reference images (PNG format),
* dimensions of output images
* Output: output images (PNG format)
*
* pcaviewsB: creates 1 image displaying the 3 principal views of the
* RGB cube of a color image, the principal axis are computed
* by Principal Components Analysis of the distribution of colors
* of a reference image
* Input: input and reference images (PNG format),
* dimensions of the views displayed in the output image
* Output: output image (PNG format)
*
* densityimage: given an input color image creates a grey-scale image
* where each pixel is displayed with a grey level
* proportional to the density of its color in the input image
* Input: color image (PNG format)
* Output: density image (grey-level image, PNG format)
*
* densityviews: creates 1 image displaying the 3 principal views of the
* RGB cube of a color image, each color point is displayed
* with a grey level proportional to its density in the color
* cube (lighter for higher densities), the principal axis are
* computed by Principal Components Analysis of the
* distribution of colors of a reference image. Color
* densities are either computed or passed as an input image.
* Input: input and reference images (PNG format),
* dimensions of the views displayed in the output image,
* densities image (optional)
* Output: output image (PNG format)
*
* mergeimages: creates an output image (out) from two inputs (in1, in2)
* such that out=in1 except at pixels with (0, 0, 0) RGB value,
* which are replaced by pixels of in2
* Input: input images (PNG format)
* Output: output image (PNG format)
*
* RGBviewsparams: computes parameters (position and coordinates vectors)
* of a set of projection planes for displaying different
* views of the RGB color cube. Stores results in a text file
* Output: text file
*
* RGBviews: creates a set of images displaying different views of the RGB
* cube of a color image
* Input: input image (PNG format) and text file with information
* about the projection planes,
* dimensions of the output image,
* flag displayDensity (if displayDensity=1, each color point
* is displayed with a grey level value proportional to its
* density in RGB space, else display point with its original
* color),
* densities image (optional, PNG format)
* Output: output image (PNG format)
*
* combineviews: creates a set of output images from a set of three input
* images, for each output image the input images are arranged
* in two rows: the first row displays two inputs and the
* second row the third input,
* Input: text file with information about the number of output
* images,
* input images (PNG format)
* Output: output images (PNG format)
*
* computeRMSE: compute Root Mean Squared Error (RMSE)
* and Average Distance (dmean)
* between two sets of colors in RGB space
* Input: input image (PNG format)
* Output: RMSE and dmean values
*
* countcolors: count the number of different colors in an image
* Input: input image (PNG format)
* Output: number of colors of the input image
*
* RGB2VRML2: creates a file in VRML 2.0 format containing the list of color
* points of an input image. Each point is either displayed with
* its original RGB color or with a gray level value proportional
* to the density of the color.
* Input: input image (PNG format),
* flag displayDensity (if displayDensity=1, each color point
* is displayed with a grey level value proportional to its
* density in RGB space, else display point with its original
* color),
* densities image (optional, PNG format)
* Output: VRML2.0 text file
*
*
* @see rgbprocess_lib.cpp
*/
int main(int argc, char *const *argv)
{
unsigned char *img, *img2, *img3, *imgOut1=NULL, *imgOut2=NULL,
*imgOut3=NULL;
int wOut, hOut;
size_t nx = 0, ny = 0, nc = 0;
/* "-v" option : version info */
if (2 <= argc && 0 == strcmp("-v", argv[1]))
{
fprintf(stdout, "%s version " VERSION
", compiled " __DATE__ "\n", argv[0]);
return EXIT_SUCCESS;
}
/* wrong number of parameters : simple help info */
if (argc < 2)
{
fprintf(stderr,
"usage : %s [filter in.png out.png]\n",
argv[0]);
fprintf(stderr,
"usage : %s [rmisolated in.png out.png]\n",
argv[0]);
fprintf(stderr,
"usage : %s [pcaviews in1.png in2.png out1.png out2.png \
out3.png wOut hOut]\n",
argv[0]);
fprintf(stderr,
"usage : %s [pcaviewsB in1.png in2.png out123.png \
wOut hOut]\n",
argv[0]);
fprintf(stderr,
"usage : %s [densityimage in1.png density.png\n",
argv[0]);
fprintf(stderr,
"usage : %s [densityviews in1.png in2.png out123.png \
wOut hOut [optional: density.png]]\n",
argv[0]);
fprintf(stderr,
"usage : %s [mergeimages in1.png in2.png out.png]\n",
argv[0]);
fprintf(stderr,
"usage : %s [RGBviewsparams nameparams.txt]\n",
argv[0]);
fprintf(stderr,
"usage : %s [RGBviews in1.png nameparams.txt nameviews \
wOut hOut displaydensity=0/1 [optional: density.png]]\n",
argv[0]);
fprintf(stderr,
"usage : %s [combineviews nameparams.txt nameviews1 \
nameview2 nameview3 nameviewsOut]\n",
argv[0]);
fprintf(stderr,
"usage : %s [computeRMSE in1.png in2.png]\n",
argv[0]);
fprintf(stderr,
"usage : %s [countcolors in1.png]\n",
argv[0]);
fprintf(stderr,
"usage : %s [RGB2VRML2 in1.png out.wrl displaydensity=0/1 \
[optional: density.png]]\n",
argv[0]);
return EXIT_FAILURE;
}
if (!strcmp(argv[1], "filter")) {
/* read the PNG input image */
img = io_png_read_u8_rgb(argv[2], &nx, &ny);
if (!img)
{
fprintf(stderr, "Error reading input image or not a color image!\n");
return EXIT_FAILURE;
}
/* process input image */
rgbprocess(img, NULL, NULL, nx, ny, img, NULL, NULL, nx, ny, "filter");
/* write the PNG output image */
nc=3; /*color image*/
io_png_write_u8(argv[3], img, nx, ny, nc);
/* free the memory */
free(img);
}
if (!strcmp(argv[1], "rmisolated")) {
/* read the PNG input image */
img = io_png_read_u8_rgb(argv[2], &nx, &ny);
if (!img)
{
fprintf(stderr, "Error reading input image or not a color image!\n");
return EXIT_FAILURE;
}
/* process input image */
rgbprocess(img, NULL, NULL, nx, ny, img, NULL, NULL,
nx, ny, "rmisolated");
/* write the PNG output image */
nc=3; /*color image*/
io_png_write_u8(argv[3], img, nx, ny, nc);
/* free the memory */
free(img);
}
if (!strcmp(argv[1], "pcaviews")) {
/* read the PNG input image */
img = io_png_read_u8_rgb(argv[2], &nx, &ny);
if (!img)
{
fprintf(stderr, "Error reading input image or not a color image!\n");
return EXIT_FAILURE;
}
/* read the PNG input image */
img2 = io_png_read_u8_rgb(argv[3], &nx, &ny);
if (!img2)
{
fprintf(stderr,
"Error reading reference input image or not a color image!\n");
free(img);
return EXIT_FAILURE;
}
wOut = atoi(argv[7]);
hOut = atoi(argv[8]);
imgOut1=(unsigned char *) malloc(3*wOut*hOut);
imgOut2=(unsigned char *) malloc(3*wOut*hOut);
imgOut3=(unsigned char *) malloc(3*wOut*hOut);
/* process input image */
rgbprocess(img, img2, NULL, nx, ny,
imgOut1, imgOut2, imgOut3, wOut, hOut, "pcaviews");
/* write the PNG output image */
nc=3; /*color image*/
io_png_write_u8(argv[4], imgOut1, wOut, hOut, nc);
io_png_write_u8(argv[5], imgOut2, wOut, hOut, nc);
io_png_write_u8(argv[6], imgOut3, wOut, hOut, nc);
/* free the memory */
free(img);
free(img2);
free(imgOut1);
free(imgOut2);
free(imgOut3);
}
if (!strcmp(argv[1], "pcaviewsB")) {
/* read the PNG input image */
img = io_png_read_u8_rgb(argv[2], &nx, &ny);
if (!img)
{
fprintf(stderr, "Error reading input image or not a color image!\n");
return EXIT_FAILURE;
}
/* read the PNG input image */
img2 = io_png_read_u8_rgb(argv[3], &nx, &ny);
if (!img2)
{
fprintf(stderr,
"Error reading reference input image or not a color image!\n");
free(img);
return EXIT_FAILURE;
}
wOut = atoi(argv[5]);
hOut = atoi(argv[6]);
imgOut1=(unsigned char *) malloc(3*3*wOut*hOut);
/* process input image */
rgbprocess(img, img2, NULL, nx, ny,
imgOut1, NULL, NULL, wOut, hOut, "pcaviewsB");
/* write the PNG output image */
nc=3; /*color image*/
io_png_write_u8(argv[4], imgOut1, 3*wOut, hOut, nc);
/* free the memory */
free(img);
free(img2);
free(imgOut1);
}
if (!strcmp(argv[1], "densityimage")) {
/* read the PNG input image */
img = io_png_read_u8_rgb(argv[2], &nx, &ny);
if (!img)
{
fprintf(stderr, "Error reading input image or not a color image!\n");
return EXIT_FAILURE;
}
imgOut1=(unsigned char *) malloc(nx*ny);
/* process input image */
rgbprocess(img, NULL, NULL, nx, ny,
imgOut1, NULL, NULL, nx, ny, "densityImage");
/* write the PNG output image */
nc=1; /*grey-scale image*/
io_png_write_u8(argv[3], imgOut1, nx, ny, nc);
/* free the memory */
free(img);
free(imgOut1);
}
if (!strcmp(argv[1], "densityviews")) {
/* read the PNG input image */
img = io_png_read_u8_rgb(argv[2], &nx, &ny);
if (!img)
{
fprintf(stderr, "Error reading input image or not a color image!\n");
return EXIT_FAILURE;
}
/* read the PNG input image */
img2 = io_png_read_u8_rgb(argv[3], &nx, &ny);
if (!img2)
{
fprintf(stderr,
"Error reading reference input image or not a color image!\n");
free(img);
return EXIT_FAILURE;
}
wOut = atoi(argv[5]);
hOut = atoi(argv[6]);
img3=NULL;
if (argc > 7) {
/* read the PNG density image */
img3 = io_png_read_u8_gray(argv[7], &nx, &ny);
if (!img3)
{
fprintf(stderr, "Error reading density input image!\n");
free(img);
free(img2);
return EXIT_FAILURE;
}
}
imgOut1=(unsigned char *) malloc(3*3*wOut*hOut);
/* process input image */
rgbprocess(img, img2, img3, nx, ny,
imgOut1, NULL, NULL, wOut, hOut, "densityviews");
/* write the PNG output image */
nc=3; /*color image*/
io_png_write_u8(argv[4], imgOut1, 3*wOut, hOut, nc);
/* free the memory */
free(img);
free(img2);
if (img3) free(img3);
free(imgOut1);
}
if (!strcmp(argv[1], "mergeimages")) {
/* read the PNG input image */
img = io_png_read_u8_rgb(argv[2], &nx, &ny);
if (!img)
{
fprintf(stderr, "Error reading input image or not a color image!\n");
return EXIT_FAILURE;
}
/* read the PNG input image */
img2 = io_png_read_u8_rgb(argv[3], &nx, &ny);
if (!img2)
{
fprintf(stderr,
"Error reading reference input image or not a color image!\n");
free(img);
return EXIT_FAILURE;
}
imgOut1=(unsigned char *) malloc(3*nx*ny);
/* process input image */
rgbprocess(img, img2, NULL, nx, ny,
imgOut1, NULL, NULL, nx, ny, "mergeimages");
/* write the PNG output image */
nc=3; /*color image*/
io_png_write_u8(argv[4], imgOut1, nx, ny, nc);
/* free the memory */
free(img);
free(img2);
free(imgOut1);
}
if (!strcmp(argv[1], "RGBviewsparams")) {
RGBviewsparams(argv[2]);
}
if (!strcmp(argv[1], "RGBviews")) {
/* read the PNG input image */
img = io_png_read_u8_rgb(argv[2], &nx, &ny);
if (!img)
{
fprintf(stderr, "Error reading input image or not a color image!\n");
return EXIT_FAILURE;
}
/* views size */
int wview, hview;
sscanf(argv[5], "%i", &wview);
sscanf(argv[6], "%i", &hview);
imgOut1=(unsigned char *) malloc(3*wview*hview);
/* Compute densities, if necessary */
unsigned char *Idsty=NULL;
int displayDensity;
sscanf(argv[7], "%i", &displayDensity);
if (displayDensity == 1) {
if (argc > 8) {
/* read the PNG density image */
Idsty = io_png_read_u8_gray(argv[8], &nx, &ny);
if (!Idsty) {
fprintf(stderr, "Error reading density input image!\n");
free(img);
free(imgOut1);
return EXIT_FAILURE;
}
} else {
printf("compute density\n");
Idsty=(unsigned char *) malloc(nx*ny);
rgbprocess(img, NULL, NULL, nx, ny,
Idsty, NULL, NULL, 0, 0, "densityImage");
}
}
const char *nameviews=argv[4];
char nameview[128];
/* Read info from params file */
FILE *params;
int nviews;
float P[3], u[3], v[3];
params=fopen(argv[3], "r");
int nread;
nread=fscanf(params, "%i", &nviews);
for (int n=0; n < nviews; n++) {
nread=fscanf(params, "%f %f %f %f %f %f %f %f %f",
&P[0], &P[1], &P[2], &u[0], &u[1], &u[2],
&v[0], &v[1], &v[2]);
getRGBview(img, nx, ny, Idsty, imgOut1, wview, hview, P, u, v);
/* write the PNG output image */
nc=3; /*color image*/
sprintf(nameview, "%s_%i.png", nameviews, 100+n);
io_png_write_u8(nameview, imgOut1, wview, hview, nc);
}
fclose(params);
/* free the memory */
free(img);
free(imgOut1);
if (Idsty) free(Idsty);
}
if (!strcmp(argv[1], "combineviews")) {
int nviews;
FILE *params=fopen(argv[2], "r");
int nread;
nread=fscanf(params, "%i", &nviews);
fclose(params);
int wview, hview;
unsigned char *img1, *img2, *img3, *imgOut1;
imgOut1=NULL;
const char *nameviews1=argv[3];
const char *nameviews2=argv[4];
const char *nameviews3=argv[5];
const char *nameviewsOut=argv[6];
char nameview1[128], nameview2[128], nameview3[128], nameviewOut[128];
for (int n=0; n < nviews; n++) {
sprintf(nameview1, "%s_%i.png", nameviews1, 100+n);
sprintf(nameview2, "%s_%i.png", nameviews2, 100+n);
sprintf(nameview3, "%s_%i.png", nameviews3, 100+n);
sprintf(nameviewOut, "%s_%i.png", nameviewsOut, 100+n);
/* read the PNG input images */
img1 = io_png_read_u8_rgb(nameview1, &nx, &ny);
if (!img1) {
fprintf(stderr, "Error reading input image or not a color image!\n");
return EXIT_FAILURE;
}
img2 = io_png_read_u8_rgb(nameview2, &nx, &ny);
if (!img2)
{
fprintf(stderr, "Error reading input image or not a color image!\n");
free(img1);
return EXIT_FAILURE;
}
img3 = io_png_read_u8_rgb(nameview3, &nx, &ny);
if (!img3)
{
fprintf(stderr, "Error reading input image or not a color image!\n");
free(img1);
free(img2);
return EXIT_FAILURE;
}
wview=(int) nx;
hview=(int) ny;
if (!imgOut1) imgOut1=(unsigned char *) malloc(3*(2*wview)*(2*hview));
// process input image
rgbprocess(img1, img2, img3, nx, ny,
imgOut1, NULL, NULL, 0, 0, "combineviewsB");
// write the PNG output image
nc=3; //color image
io_png_write_u8(nameviewOut, imgOut1, 2*nx, 2*ny, nc);
/* free the memory */
free(img1);
free(img2);
free(img3);
}
free(imgOut1);
}
if (!strcmp(argv[1], "computeRMSE")) {
/* read the PNG input image */
img = io_png_read_u8_rgb(argv[2], &nx, &ny);
if (!img) {
fprintf(stderr, "Error reading input image or not a color image!\n");
return EXIT_FAILURE;
}
/* read the PNG input image */
img2 = io_png_read_u8_rgb(argv[3], &nx, &ny);
if (!img2) {
fprintf(stderr,
"Error reading reference input image or not a color image!\n");
free(img);
return EXIT_FAILURE;
}
float rmse, dmean;
rmse=computeRMSE(img, img2, nx, ny, dmean);
printf("The Root Mean Square Error (RMSE) between the original and \
filtered color clouds is: %2.2f\n", rmse);
printf("The Average Distance (dmean) between the original and filtered \
color clouds is: %2.2f\n", dmean);
/* free the memory */
free(img);
free(img2);
}
if (!strcmp(argv[1], "countcolors")) {
/* read the PNG input image */
img = io_png_read_u8_rgb(argv[2], &nx, &ny);
if (!img)
{
fprintf(stderr, "Error reading input image or not a color image!\n");
return EXIT_FAILURE;
}
int nRGB;
nRGB=countcolors(img, nx, ny);
printf("Number of colors of filtered image: %i\n", nRGB);
/* free the memory */
free(img);
}
if (!strcmp(argv[1], "RGB2VRML2")) {
/* read the PNG input image */
img = io_png_read_u8_rgb(argv[2], &nx, &ny);
if (!img)
{
fprintf(stderr, "Error reading input image or not a color image!\n");
return EXIT_FAILURE;
}
/* Compute densities, if necessary */
unsigned char *Idsty=NULL;
int displayDensity;
sscanf(argv[4], "%i", &displayDensity);
if (displayDensity == 1) {
if (argc > 5) {
/* read the PNG density image */
Idsty = io_png_read_u8_gray(argv[5], &nx, &ny);
if (!Idsty) {
fprintf(stderr, "Error reading density input image!\n");
free(img);
return EXIT_FAILURE;
}
} else {
printf("compute density\n");
Idsty=(unsigned char *) malloc(nx*ny);
rgbprocess(img, NULL, NULL, nx, ny,
Idsty, NULL, NULL, 0, 0, "densityImage");
}
}
RGB2VRML2(img, nx, ny, Idsty, argv[3]);
/* free the memory */
free(img);
if (Idsty) free(Idsty);
}
return EXIT_SUCCESS;
}

Computing file changes ...