Hi Juan,
I just tried to write a "re-bayer" procedure that changes an RGB image with the original bayer pattern into an image with a single color channel. The result is strange: The crop operation, which is part of the program, produces an a pattern where I get very different R-values from the original. It appears that the function doCrop() does some kind of interpolation that I did not expect.
See the attached screenshot and test code. The left image contains the simulated bayer pattern, the lower right one is the cropped image. The different tones of pixels are obvious.
Kind regards,
Georg
#include <pjsr/ImageOp.jsh>
#include <pjsr/SampleType.jsh>
#include <pjsr/UndoFlag.jsh>
#include <pjsr/ColorSpace.jsh>
// compute target rectangle for image, using the central part defined by lenght factor
function computeTargetRect(image, lengthFactor){
var width=image.width;
var height=image.height;
var newWidth=Math.round(width*lengthFactor);
var newHeight=Math.round(height*lengthFactor);
var targetRect=new Rect(newWidth,newHeight);
targetRect.moveTo((width-newWidth)/2,(height-newHeight)/2);
return targetRect;
} //computeTargetRect()
function cropImage(image1, lengthFactor){
var targetRect=computeTargetRect(image1,lengthFactor);
// Cropped images
image1.resetSelections();
var cropped1 = new Image( targetRect.width, targetRect.height,
image1.numberOfChannels, image1.colorSpace,
image1.bitsPerSample, image1.sampleType);
image1.selectedRect = targetRect;
cropped1.apply( image1 );
image1.resetSelections();
return cropped1;
}
// function to transform it to single color
function doSingleChannel(image){
if (image.numberOfChannels==1){
return image;
}
var res=new Image( image.width, image.height,
1, ColorSpace_Gray,
image.bitsPerSample, image.sampleType);
image.resetSelections();
res.fill(0);
for(var chan=0;chan<image.numberOfChannels;++chan){
image.selectedChannel=chan;
res.apply(image,ImageOp_Add);
}
image.resetSelections();
return res;
} // doSingleChannel
function main1() {
// create bayer pattern image
var image=new Image(3908,2602,3,ColorSpace_RGB,16,SampleType_Integer);
for (var row=0;row<image.height;++row){
for (var col=0; col<image.width;++col){
//simulate bayer pattern
if(row%2==0){
if (col%2==0){
image.setSample(0.2,col,row,0);
image.setSample(0,col,row,1);
image.setSample(0,col,row,2);
}else{
image.setSample(0.2,col,row,1);
image.setSample(0,col,row,0);
image.setSample(0,col,row,2);
}
}else{
if (col%2==0){
image.setSample(0.2,col,row,1);
image.setSample(0,col,row,0);
image.setSample(0,col,row,2);
}else{
image.setSample(0.2,col,row,2);
image.setSample(0,col,row,0);
image.setSample(0,col,row,1);
}
}
}
}
var cropped=cropImage(image,0.1);
var single=doSingleChannel(cropped);
var w1 = new ImageWindow( image.width, image.height,
image.numberOfChannels, image.bitsPerSample,
image.sampleType!=SampleType_Integer, image.colorSpace==ColorSpace_RGB,
"Bayer");
w1.mainView.beginProcess( UndoFlag_NoSwapFile );
w1.mainView.image.assign( image );
w1.mainView.endProcess();
var w2 = new ImageWindow( single.width, single.height,
single.numberOfChannels, single.bitsPerSample,
single.sampleType!=SampleType_Integer,image.colorSpace==ColorSpace_RGB,
"Single" );
w2.mainView.beginProcess( UndoFlag_NoSwapFile );
w2.mainView.image.assign( single );
w2.mainView.endProcess();
var w3 = new ImageWindow( cropped.width,cropped.height,
cropped.numberOfChannels, cropped.bitsPerSample,
cropped.sampleType!=SampleType_Integer,cropped.colorSpace==ColorSpace_RGB,
"Cropped" );
w2.mainView.beginProcess( UndoFlag_NoSwapFile );
w2.mainView.image.assign( single );
w2.mainView.endProcess();
w1.show();
w1.zoomToOptimalFit();
w2.show();
w2.zoomToOptimalFit();
w3.show();
w3.zoomToOptimalFit();
}
main1();