Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I'm trying to get a random HSV colour then convert it to RGB to be used. Does anyone have any ideas as to how I can do this? Thanks, Sam. What I've got so far: First method converts HSV to RGB with the specified values, the second method is for getting a random colour.
public static float[] HSVtoRGB(float h, float s, float v) {
float m, n, f;
int i;
float[] hsv = new float[3];
float[] rgb = new float[3];
hsv[0] = h;
hsv[1] = s;
hsv[2] = v;
if (hsv[0] == -1) {
rgb[0] = rgb[1] = rgb[2] = hsv[2];
return rgb;
}
i = (int) (Math.floor(hsv[0]));
f = hsv[0] - i;
if (i % 2 == 0) {
f = 1 - f; // if i is even
}
m = hsv[2] * (1 - hsv[1]);
n = hsv[2] * (1 - hsv[1] * f);
switch (i) {
case 6:
case 0:
rgb[0] = hsv[2];
rgb[1] = n;
rgb[2] = m;
break;
case 1:
rgb[0] = n;
rgb[1] = hsv[2];
rgb[2] = m;
break;
case 2:
rgb[0] = m;
rgb[1] = hsv[2];
rgb[2] = n;
break;
case 3:
rgb[0] = m;
rgb[1] = n;
rgb[2] = hsv[2];
break;
case 4:
rgb[0] = n;
rgb[1] = m;
rgb[2] = hsv[2];
break;
case 5:
rgb[0] = hsv[2];
rgb[1] = m;
rgb[2] = n;
break;
}
return rgb;
}
public static int randomColor() {
int hue = (int) (Math.random() * 6.0f);
int saturation = (int) (Math.random());
int brightness = (int) (Math.random());
float[] rgb = HSVtoRGB(hue, saturation, brightness);
int red = (int) (rgb[0] * 255.0f);
int green = (int) (rgb[1] * 255.0f);
int blue = (int) (rgb[2] * 255.0f);
return (red << 16) | (green << 8) | blue;
}
You can use java.awt.Color.RGBtoHSB(...) You can find the relevant documentation for it here: http://docs.oracle.com/javase/1.4.2/docs/api/java/awt/Color.html
Then it just becomes trivial of generating a random color.
int red = (int) (Math.random() * 256)
int green = (int) (Math.random() * 256)
int blue = (int) (Math.random() * 256)
Then convert directly. Note that there is also a HSBtoRGB(...) function in the same class.
Related
Does POI-Framwork support to get background color of a XSSFSimpleShape object? I looked around this class but I couldn't find the way to get its background?
Here is my code:
XSSFSimpleShape simpleObj = ...
simpleObj.getCTShape().getSpPr()... get some things named color here
simpleObj.getCTShape().getStyle().getFillRef()... get some things named colors here
This is the opposite of a trivial task. You are on the right way with CTShapeProperties. The next step is CTSolidColorFillProperties and as long we will find a CTSRgbColor all things will be easy because this is simply RGB. But there are much more possible kinds of color types as you see.
One possible color type which Excel is often using is CTSchemeColor. This color is a theme color from ThemesTable but possibly additional determined by given luminescence changing from lumMod and lumOff.
Example XML:
<a:solidFill>
<a:schemeClr val="accent4">
<a:lumMod val="60000"/>
<a:lumOff val="40000"/>
</a:schemeClr>
</a:solidFill>
Problem with this is that those luminescence changings are made in HSL. Javas java.awt.Color is only supporting HSB (aka HSV) but not HSL. So we need additional code for supporting HSL. In my example I have used the code from Rob Camick.
So knowing that all we can do the following:
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFSimpleShape;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFColor;
import org.apache.poi.xssf.model.ThemesTable;
import java.io.InputStream;
import java.io.FileInputStream;
import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTShape;
import org.openxmlformats.schemas.drawingml.x2006.main.CTShapeProperties;
import org.openxmlformats.schemas.drawingml.x2006.main.CTSolidColorFillProperties;
import org.openxmlformats.schemas.drawingml.x2006.main.STSchemeColorVal;
import java.awt.Color;
class ReadExcelShapeFillColors {
//two methods for dealing with the HSL color model
//helper method HueToRGB, see below
private static float HueToRGB(float p, float q, float h) {
if (h < 0) h += 1;
if (h > 1 ) h -= 1;
if (6 * h < 1) {
return p + ((q - p) * 6 * h);
}
if (2 * h < 1 ) {
return q;
}
if (3 * h < 2) {
return p + ( (q - p) * 6 * ((2.0f / 3.0f) - h) );
}
return p;
}
//get a new Color changed by given luminescence from lumMod and lumOff
private static Color getColorLumModandOff(Color color, int lumMod, int lumOff) {
float[] rgb = color.getRGBColorComponents( null );
float r = rgb[0];
float g = rgb[1];
float b = rgb[2];
float min = Math.min(r, Math.min(g, b));
float max = Math.max(r, Math.max(g, b));
float h = 0;
if (max == min) h = 0;
else if (max == r) h = ((60 * (g - b) / (max - min)) + 360) % 360;
else if (max == g) h = (60 * (b - r) / (max - min)) + 120;
else if (max == b) h = (60 * (r - g) / (max - min)) + 240;
float l = (max + min) / 2;
l = l * (float)lumMod/100000f + (float)lumOff/100000f;
float s = 0;
if (max == min) s = 0;
else if (l <= .5f) s = (max - min) / (max + min);
else s = (max - min) / (2 - max - min);
h = h % 360.0f;
h /= 360f;
float q = 0;
if (l < 0.5) q = l * (1 + s);
else q = (l + s) - (s * l);
float p = 2 * l - q;
r = Math.max(0, HueToRGB(p, q, h + (1.0f / 3.0f)));
g = Math.max(0, HueToRGB(p, q, h));
b = Math.max(0, HueToRGB(p, q, h - (1.0f / 3.0f)));
r = Math.min(r, 1.0f);
g = Math.min(g, 1.0f);
b = Math.min(b, 1.0f);
return new Color(r, g, b, 1.0f);
}
public static void main(String[] args) throws Exception {
InputStream inp = new FileInputStream("ExcelWithSimpleShape.xlsx");
Workbook workbook = WorkbookFactory.create(inp);
Sheet sheet = workbook.getSheetAt(0);
Drawing<? extends Shape> drawing = sheet.getDrawingPatriarch();
for (Shape shape : drawing) {
System.out.println(shape.getClass());
System.out.println(shape.getShapeName() + "_________________");
if (shape instanceof XSSFSimpleShape) { //we have a XSSFSimpleShape
XSSFWorkbook xssfworkbook = (XSSFWorkbook)workbook;
ThemesTable themesTable = xssfworkbook.getTheme();
XSSFSimpleShape xssfSimpleShape = (XSSFSimpleShape)shape;
CTShape ctShape = xssfSimpleShape.getCTShape();
CTShapeProperties ctShapeProperties = ctShape.getSpPr();
byte[] bRGB;
if (ctShapeProperties.isSetSolidFill()) { //we have a solid fill defined
CTSolidColorFillProperties ctSolidColorFillProperties = ctShapeProperties.getSolidFill();
if (ctSolidColorFillProperties.isSetSrgbClr()) { //we have a explicit given RGB color
bRGB = ctSolidColorFillProperties.getSrgbClr().getVal();
System.out.println((bRGB[0]&0xFF)+", "+(bRGB[1]&0xFF)+", "+(bRGB[2]&0xFF));
Color color = new Color(bRGB[0]&0xFF, bRGB[1]&0xFF, bRGB[2]&0xFF);
System.out.println("explicit given RGB color: " + color);
} else if (ctSolidColorFillProperties.isSetSchemeClr()) { //we have a scheme color defined in ThemesTable
int iThemeColorIdx = ctSolidColorFillProperties.getSchemeClr().getVal().intValue()-1;
System.out.println("theme color index: " + iThemeColorIdx);
//get luminescence definition
int lumMod = 100000;
int lumOff = 0;
if (ctSolidColorFillProperties.getSchemeClr().getLumModList().size() > 0)
lumMod = ctSolidColorFillProperties.getSchemeClr().getLumModList().get(0).getVal();
if (ctSolidColorFillProperties.getSchemeClr().getLumOffList().size() > 0)
lumOff = ctSolidColorFillProperties.getSchemeClr().getLumOffList().get(0).getVal();
System.out.println("lumMod: " + lumMod);
System.out.println("lumOff: " + lumOff);
XSSFColor xssfColor = themesTable.getThemeColor(iThemeColorIdx);
bRGB = xssfColor.getRGB(); //RGB color from ThemesTable
System.out.println((bRGB[0]&0xFF)+", "+(bRGB[1]&0xFF)+", "+(bRGB[2]&0xFF));
Color color = new Color(bRGB[0]&0xFF, bRGB[1]&0xFF, bRGB[2]&0xFF);
color = getColorLumModandOff(color, lumMod, lumOff); //Color changed by given lumMod and lumOff
System.out.println("scheme color: " + color);
}
} else { //we have accent1 scheme color as fill color
XSSFColor xssfColor = themesTable.getThemeColor(STSchemeColorVal.INT_ACCENT_1-1);
bRGB = xssfColor.getRGB();
System.out.println((bRGB[0]&0xFF)+", "+(bRGB[1]&0xFF)+", "+(bRGB[2]&0xFF));
Color color = new Color(bRGB[0]&0xFF, bRGB[1]&0xFF, bRGB[2]&0xFF);
System.out.println("accent1 scheme color: " + color);
}
}
}
workbook.close();
}
}
This code will get the fill colors of all shapes from first sheet of the XSSFWorkbook file which are instanceof XSSFSimpleShape as long as they are given by solid fill and are either CTSRgbColor or CTSchemeColor.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I want to implement Sobel algorithm.
First
I get gray data of the input image and put data into mGrayData:
BufferedImage mImage, mNewImage;
for (int i = 0; i < mHeight; i++) {
for (int j = 0; j < mWidth; j++) {
int rgb = mImage.getRGB(j, i);
int newRgb;
int r = (rgb >> 16) & 0xff;
int g = (rgb >> 8) & 0xff;
int b = rgb & 0xff;
int grayLevel = (r + g + b) / 3;
int gray = (grayLevel << 16) + (grayLevel << 8) + grayLevel;
mGrayData[i * mWidth + j] = gray;
}
}
Then
I calculate every point's gradient:
int[] gradient = new int[mWidth * mHeight];
for (int x=1;x<mWidth-1;x++){
for (int y=1;y<mHeight-1;y++){
int grayX = getGrayPoint(x+1,y-1)+2*getGrayPoint(x+1,y)+getGrayPoint(x+1,y+1)-
(getGrayPoint(x-1,y-1)+2*getGrayPoint(x-1,y)+getGrayPoint(x-1,y+1));
int grayY = (getGrayPoint(x-1, y+1) + 2*getGrayPoint(x,y+1)+getGrayPoint(x+1,y+1))-
(getGrayPoint(x-1,y-1) + 2*getGrayPoint(x,y-1) + getGrayPoint(x+1,y-1));
gradient[x+y*mWidth] = (int) Math.sqrt(grayX*grayX+grayY*grayY);
}
}
method gradient(x,y):
private int getGrayPoint(int x,int y){
return mGrayData[x+y*mWidth];
}
Problem
input image:
and after be fitered:
Now how can I implement this?
Edit:
I do not know about how to use gradient data.I try this:
int[] gradient = getGradient();
int maxGradient = gradient[0];
for (int i=0;i<gradient.length;i++){
if (gradient[i]>maxGradient)
maxGradient = gradient[i];
}
float scaleFactor = 255.0f / maxGradient;
for (int y = 1; y < mHeight - 1; ++y)
for (int x = 1; x < mWidth - 1; ++x)
if (Math.round(scaleFactor * gradient[y * mWidth + x]) >= mThreshold)
{
mNewImage.setRGB(x, y, 0x000000);
}else {
mNewImage.setRGB(x,y,mGrayData[y * mWidth + x]);
}
File file = new File("D:\\Documents\\Pictures\\engine3.png");
if (!file.exists()) {
file.getParentFile().mkdir();
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
ImageIO.write(mNewImage, "png", file);
} catch (IOException e) {
e.printStackTrace();
}
And I get image:
It seems that you are using wrong data for gradient calculation. Your mGrayData contains integer value 0xAXXX, where A is alpha and X is gray value (computed as average of r,g,b).
Instead use
mGrayData[i * mWidth + j] = average;
2nd edit:
Suggestion:
private int getGrayPoint(int x,int y){
return mGrayData[x+y*mWidth] & 0xff;
}
I want to implement image processing in my project. I had used JLabs library. I want to do using this library following effects.
1) Deboss
2) Engrave
3) Satin etch
4) Custom shape like Cylinder.
This is sample image emboss code.
public class EmbossFilter
extends WholeImageFilter {
private static final float pixelScale = 255.9f;
private float azimuth = 2.3561945f;
private float elevation = 0.5235988f;
private boolean emboss = false;
private float width45 = 3.0f;
public void setAzimuth(float azimuth) {
this.azimuth = azimuth;
}
public float getAzimuth() {
return this.azimuth;
}
public void setElevation(float elevation) {
this.elevation = elevation;
}
public float getElevation() {
return this.elevation;
}
public void setBumpHeight(float bumpHeight) {
this.width45 = 3.0f * bumpHeight;
}
public float getBumpHeight() {
return this.width45 / 3.0f;
}
public void setEmboss(boolean emboss) {
this.emboss = emboss;
}
public boolean getEmboss() {
return this.emboss;
}
#Override
protected int[] filterPixels(int width, int height, int[] inPixels, Rect transformedSpace) {
int index = 0;
int[] outPixels = new int[width * height];
int bumpMapWidth = width;
int bumpMapHeight = height;
int[] bumpPixels = new int[bumpMapWidth * bumpMapHeight];
int i = 0;
while (i < inPixels.length) {
bumpPixels[i] = PixelUtils.brightness(inPixels[i]);
++i;
}
int Lx = (int) (Math.cos(this.azimuth) * Math.cos(this.elevation) * 255.89999389648438);
int Ly = (int) (Math.sin(this.azimuth) * Math.cos(this.elevation) * 255.89999389648438);
int Lz = (int) (Math.sin(this.elevation) * 255.89999389648438);
int Nz = (int) (1530.0f / this.width45);
int Nz2 = Nz * Nz;
int NzLz = Nz * Lz;
int background = Lz;
int bumpIndex = 0;
int y = 0;
while (y < height) {
int s1 = bumpIndex;
int s2 = s1 + bumpMapWidth;
int s3 = s2 + bumpMapWidth;
int x = 0;
while (x < width) {
int shade;
if (y != 0 && y < height - 2 && x != 0 && x < width - 2) {
int NdotL;
int Nx = bumpPixels[s1 - 1] + bumpPixels[s2 - 1] + bumpPixels[s3 - 1] - bumpPixels[s1 + 1] - bumpPixels[s2 + 1] - bumpPixels[s3 + 1];
int Ny = bumpPixels[s3 - 1] + bumpPixels[s3] + bumpPixels[s3 + 1] - bumpPixels[s1 - 1] - bumpPixels[s1] - bumpPixels[s1 + 1];
shade = Nx == 0 && Ny == 0 ? background : ((NdotL = Nx * Lx + Ny * Ly + NzLz) < 0 ? 0 : (int) ((double) NdotL / Math.sqrt(Nx * Nx + Ny * Ny + Nz2)));
} else {
shade = background;
}
if (this.emboss) {
int rgb = inPixels[index];
int a = rgb & -16777216;
int r = rgb >> 16 & 255;
int g = rgb >> 8 & 255;
int b = rgb & 255;
r = r * shade >> 8;
g = g * shade >> 8;
b = b * shade >> 8;
outPixels[index++] = a | r << 16 | g << 8 | b;
} else {
outPixels[index++] = -16777216 | shade << 16 | shade << 8 | shade;
}
++x;
++s1;
++s2;
++s3;
}
++y;
bumpIndex += bumpMapWidth;
}
return outPixels;
}
public String toString() {
return "Stylize/Emboss...";
}
}
How to achieve image effects using Jhlabs library or any other way please share with me.
My goal is that I have an image which spits out a bitmap. Now I want display the average color of the image as one giant pixel. This is a fairly simple task, just use bufferImage and get the bitmap which I take each red, green, and blue value, add it all up and then divide by the picture's resolution.
The thing is after doing this, I want to break the image into four quadrants and take the average color for each quadrant and display it. And again break each of the four quadrants and do the same. The issue I am facing is that I am using a recursive statement that does the following:
private static void getBlockAverage(int startHeight, int endHeight, int startWidth, int endWidth, BufferedImage img, BufferedImage blockImg, Color oldAvg) {
if(endHeight <= startHeight || endWidth <= startWidth) {
counter++;
return;
}
// get quadrant pixel average and display, I deleted this portion of the code just to keep things compact
getBlockAverage(startHeight, (startHeight + endHeight)/2, startWidth, (startWidth + endWidth)/2, img, blockImg, color);
getBlockAverage((startHeight + endHeight)/2, endHeight, startWidth, (startWidth + endWidth)/2, img, blockImg, color);
getBlockAverage(startHeight, (startHeight + endHeight)/2, (startWidth+endWidth)/2, endWidth, img, blockImg, color);
getBlockAverage((startHeight+endHeight)/2, endHeight, (startWidth+endWidth)/2, endWidth, img, blockImg, color);
}
It is quite easy to see that this is not what I want as the recursive statement will keep executing getBlockAverage(startHeight, (startHeight + endHeight)/2, startWidth, (startWidth + endWidth)/2, img, blockImg, color); first until it is done and then move onto the next one. This is not what I want. I want the image to be broken down into 4 quadrants and then each of those quadrants gets broken down until all quadrants are broken down and continue.
So for example:
Starting off with 1 quadrants, break into 4. Now for quadrant 1, break that into 4, now quadrant 2, break that into 4, now quadrant 3, break that into 4, now quadrant 4, break that into 4.
Now that I am thinking about it, I feel like I should use some sort of for loop with a cap on the number of iterations but I am not sure how to do that.
I tend to agree with you. I think I would place this method into loop as well but also make the method return the average color for each quadrant into an single dimensional Array with the thought of each Array index is a quadrant number and and the actual element for that index contains the color for that particular quadrant. This way you can work with all the pertinent information that is acquired later on. I would at least get it going and then optimize it once it's working the way I want. Well, that's how I do it anyways :P
Of course I'm assuming throughout all this that the Quadrant dissection flow is something similar to what I show in the image below:
Here is what I would do:
Change the getBlockAverage() method so that it returns a Color...
private static Color getBlockAverage(int startHeight, int endHeight, int startWidth,
int endWidth, BufferedImage img, BufferedImage blockImg, Color oldAvg) {
// get quadrant pixel average color and return it
// with whatever code you've been using....
return theQuadrantAverageColor;
}
then I would create another method which contains our loop, image quadrants dissectional dimensions, and calls to the getBlockAverage() method while the loop is well...looping and for every loop cycle place the returned color from the getBlockAverage() method into a per-established Color Array:
private static void getQuadrantsColorAverages(Color[] quadrantColors, BufferedImage img) {
// Decalre and Initialize required variables.
BufferedImage wrkImg = img;
BufferedImage blockImg = null; //?????
int imgWidth = wrkImg.getWidth();
int imgHeight = wrkImg.getHeight();
int startHeight = 0;
int endHeight = 0;
int startWidth = 0;
int endWidth = 0;
Color oldAvg = null;
int quadCount = 1;
// Start our loop and and continue it until our counter
// variable named quadCount goes over 20....
while (quadCount <= 20) {
// Handle dissectional dimensions (in pixels)
// for quadrants 1 to 20 as layed out within
// the supplied image to this forum post.
switch (quadCount) {
// Quadrant 1
case 1:
startHeight = 0; endHeight = (imgHeight / 2);
startWidth = 0; endWidth = (imgWidth / 2);
// Quadrant 2
case 2:
startWidth = (endWidth + 1); endWidth = imgWidth;
break;
// Quadrant 3
case 3:
startHeight = (endHeight + 1); endHeight = imgHeight;
startWidth = 0; endWidth = (imgWidth / 2);
break;
// Quadrant 4
case 4:
startWidth = (endWidth + 1); endWidth = imgWidth;
break;
// Quadrant 5
case 5:
startHeight = 0; endHeight = (imgHeight / 4);
startWidth = 0; endWidth = (imgWidth / 4);
break;
// Quadrant 6
case 6:
startWidth = (endWidth + 1); endWidth = (imgWidth / 2);
break;
// Quadrant 7
case 7:
startHeight = (endHeight + 1); endHeight = (imgHeight / 2);
startWidth = 0; endWidth = (imgWidth / 4);
break;
// Quadrant 8
case 8:
startWidth = (endWidth + 1); endWidth = (imgWidth / 2);
break;
// Quadrant 9
case 9:
startHeight = 0; endHeight = (imgHeight / 4);
startWidth = (endWidth + 1); endWidth = ((imgWidth / 4) * 3);
break;
// Quadrant 10
case 10:
startWidth = (endWidth + 1); endWidth = imgWidth;
break;
// Quadrant 11
case 11:
startHeight = (imgHeight / 4); endHeight = (imgHeight / 2);
startWidth = (imgWidth / 2); endWidth = ((imgWidth / 4) * 3);
break;
// Quadrant 12
case 12:
startWidth = (endWidth + 1); endWidth = imgWidth;
break;
// Quadrant 13
case 13:
startHeight = (imgHeight / 2); endHeight = ((imgHeight / 4) * 3);
startWidth = 0; endWidth = (imgWidth / 4);
break;
// Quadrant 14
case 14:
startWidth = (endWidth + 1); endWidth = (imgWidth / 2);
break;
// Quadrant 15
case 15:
startHeight = (endHeight + 1); endHeight = imgHeight;
startWidth = 0; endWidth = (imgWidth / 4);
break;
// Quadrant 16
case 16:
startWidth = (endWidth + 1); endWidth = (imgWidth / 2);
break;
// Quadrant 17
case 17:
startHeight = (imgHeight / 2); endHeight = ((imgHeight / 4) * 3);
startWidth = (imgWidth / 2); endWidth = ((imgWidth / 4) * 3);
break;
// Quadrant 18
case 18:
startWidth = (endWidth + 1); endWidth = imgWidth;
break;
// Quadrant 19
case 19:
startHeight = (endHeight + 1); endHeight = imgHeight;
startWidth = (imgWidth / 2); endWidth = ((imgWidth / 4) * 3);
break;
// Quadrant 20
case 20:
startWidth = (endWidth + 1); endWidth = imgWidth;
break;
}
// Maintain the oldAvg Color variable
oldAvg = getBlockAverage(startHeight, endHeight, startWidth,
endWidth, img, blockImg, oldAvg);
// We subtract 1 from quadCount below to accomodate
// our Array indexing which must start at 0.
quadrantColors[quadCount - 1] = oldAvg;
// increment our quadrant counter by 1.
quadCount++;
}
}
Then from somewhere in your application I would initiate all this like so:
// We declare our array to handle 20 elements since
// the image will be dissected into 20 quadrants.
Color[] quadrantColors = new Color[20];
BufferedImage img = null;
// Fill our Color Array...
getQuadrantsColorAverages(quadrantColors, img);
// Let's see what we managed to get....
for (int i = 0; i < quadrantColors.length; i++) {
Color clr = quadrantColors[i];
int red = clr.getRed();
int green = clr.getGreen();
int blue = clr.getBlue();
System.out.println("The average color for Quadrant #" +
(i + 1) + " is: RGB[" + red + "," + green + "," + blue + "]");
}
Well...that's it QQCompi. I hope it sort of helps you out a little.
This question already has answers here:
Java: how to convert RGB color to CIE Lab
(6 answers)
Closed 6 years ago.
import java.awt.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.ImageIO;
public class ConvertRGBtoLAB {
public static void main(String[] args) {
//get input image
String fileName = "IMG_7990.jpg";
//read input image
BufferedImage image = null;
try
{
image = ImageIO.read(new File(fileName));
}
catch (IOException e)
{
e.printStackTrace();
}
//setup result image
int sizeX = image.getWidth();
int sizeY = image.getHeight();
float r, g, b, X, Y, Z, fx, fy, fz, xr, yr, zr;
float ls, as, bs;
float eps = 216.f/24389.f;
float k = 24389.f/27.f;
float Xr = 0.964221f; // reference white D50
float Yr = 1.0f;
float Zr = 0.825211f;
for (int y = 0; y < image.getHeight(); y++) {
for (int x = 0; x < image.getWidth(); x++) {
int c = image.getRGB(x,y);
int R= (c & 0x00ff0000) >> 16;
int G = (c & 0x0000ff00) >> 8;
int B = c & 0x000000ff;
r = R/255.f; //R 0..1
g = G/255.f; //G 0..1
b = B/255.f; //B 0..1
// assuming sRGB (D65)
if (r <= 0.04045)
r = r/12;
else
r = (float) Math.pow((r+0.055)/1.055,2.4);
if (g <= 0.04045)
g = g/12;
else
g = (float) Math.pow((g+0.055)/1.055,2.4);
if (b <= 0.04045)
b = b/12;
else
b = (float) Math.pow((b+0.055)/1.055,2.4);
X = 0.436052025f*r + 0.385081593f*g + 0.143087414f *b;
Y = 0.222491598f*r + 0.71688606f *g + 0.060621486f *b;
Z = 0.013929122f*r + 0.097097002f*g + 0.71418547f *b;
// XYZ to Lab
xr = X/Xr;
yr = Y/Yr;
zr = Z/Zr;
if ( xr > eps )
fx = (float) Math.pow(xr, 1/3.);
else
fx = (float) ((k * xr + 16.) / 116.);
if ( yr > eps )
fy = (float) Math.pow(yr, 1/3.);
else
fy = (float) ((k * yr + 16.) / 116.);
if ( zr > eps )
fz = (float) Math.pow(zr, 1/3.);
else
fz = (float) ((k * zr + 16.) / 116);
ls = ( 116 * fy ) - 16;
as = 500*(fx-fy);
bs = 200*(fy-fz);
int Ls = (int) (2.55* ls + .5);
int As = (int) (as + .5);
int Bs = (int) (bs + .5);
int lab = 0xFF000000 + (Ls << 16) + (As << 8) + Bs; // and reassign
image.setRGB(x, y, lab);
}
}
//write new image
File outputfile = new File("lab.png");
try {
// png is an image format (like gif or jpg)
ImageIO.write(image, "png", outputfile);
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
Hello,
I am trying to turn a RGB image into CIELAB colour space(LAB) I get an output but I have no idea what it is supposed to look like.
Can anyone point me to a already existing image converter or confirm that I have done this correctly?
Thanks!
I personally use this site as quick reference on conversion formulas between common color spaces.
OpenCV have functions for conversions between different color spaces. Look at my other answer here. This is in C, bout you can easily check you code.
You will want to ensure that Ls, As, and Bs are clamped to the range 0 to 255. The statement you have to combine them into a single int will allow an out-of-bounds value to affect the other values.