Algorithm
Problem Name: beecrowd | 2630
Problem Link: https://www.beecrowd.com.br/judge/en/problems/view/2630
Greyscale
By Edson Alves da Costa Júnior, UNB Brazil
Timelimit: 1
Some image processing algorithms require preprocessing in which it is necessary to turn a color image into a greyscale image. This conversion can be done in several ways, depending on the result you want to achieve.
To preserve the perception of basic colors by the human eye, an appropriate conversion would be to take 30% of the red component (R), 59% of the green component (G) and 11% of the blue component (B). In mathematical terms:
P = 0, 30R + 0, 59G + 0, 11B
Other possible approaches would be to determine the value of P through the arithmetic mean of the three components or assign P to the highest or the lowest values among the three components.
Given the RGB components of one pixel of the color image, determine the value of pixel P of the corresponding gray scale image, determining the conversion to be used. Neglect the decimal part of the result, if it exists.
Input
The input in T (1 ≤ T ≤ 100) test cases, where the value of T is given in the first line of the input. Each test case consists of two lines: the first line contains the conversion to be used: eye for the first approach described, mean for the arithmetic mean, max for the largest component value and min for the lowest component value. The second line contains the R, G, B (0 ≤ R, G, B ≤ 255) values of the colored image pixel.
Output
For each test case the following message "Caso #t: P" should be printed, where P is the gray level of the pixel of the grayscale image after the conversion of the colored image pixel. This message must be followed by a line break.
Input Sample | Output Sample |
3 min 35 70 35 mean 10 74 181 eye 23 78 197 |
Caso #1: 35 Caso #2: 88 Caso #3: 74 |
Code Examples
#1 Code Example with C Programming
Code -
C Programming
#include <stdio.h>
#include <string.h>
int main(void)
{
int n, i, j, a[3];
char str[50];
scanf("%i", &n);
for (i=0; i < n; ++i)
{
scanf("%s", str);
for (j=0; j < 3; ++j)
scanf("%i", &a[j]);
if (strcmp(str, "min")==0)
{
int min=a[0];
for (j=0; j < 3; ++j)
{
if (a[j] < min)
min=a[j];
}
printf("Caso #%i: %i\n", i+1, min);
}
else if (strcmp(str, "max")==0)
{
int max=a[0];
for (j=0; j < 3; ++j)
{
if (a[j] > max)
max=a[j];
}
printf("Caso #%i: %i\n", i+1, max);
}
else if (strcmp(str, "mean")==0)
{
int sum=0;
for (j=0; j < 3; ++j)
sum+=a[j];
printf("Caso #%i: %i\n", i+1, sum/3);
}
else if (strcmp(str, "eye")==0)
{
int eye;
eye=(a[0]*.3)+(a[1]*.59)+(a[2]*.11);
printf("Caso #%i: %i\n", i+1, eye);
}
}
return 0;
}
Copy The Code &
Try With Live Editor
Input
Output
#2 Code Example with C++ Programming
Code -
C++ Programming
#include <iostream>
#include <string.h>
using namespace std;
int main(){
int t, i;
double r, g, b, rgb;
string kind;
cin >> t;
for (i = 1;i < = t;i++) {
cin >> kind >> r >> g >> b;
if (kind == "min") {
rgb = min(r,(min(g,b)));
}else if(kind == "max"){
rgb = max(r,(max(g,b)));
}else if (kind == "eye") {
r = r*0.30;
g = g*0.59;
b = b*0.11;
rgb = r + g + b;
}else {
rgb = (r + g + b)/3;
}
//cout.precision(0);
//cout.setf(ios::fixed);
cout << "Caso #" << i << ": " << int(rgb) << endl;
}
return 0;
}
Copy The Code &
Try With Live Editor
Input
Output