RAW16 and ASI183

any question related to our SDK and driver

Moderators: yang, ray, chad

Post Reply
rcabanac
Posts: 7
Joined: Thu Dec 07, 2017 5:44 pm

RAW16 and ASI183

Post by rcabanac » Mon Oct 21, 2019 6:29 pm

Hello,

I just bought an ASI183MM Pro. I want to use the SDK to read images on my computer.
I succeeded installing, compiling and running the SDK on my MAC OS 10.15 64bit.

But, when I use the attached code, I get an image which saturate at 32767, in RAW16, instead of 65535, as if the function ASIGetDataAfterExp(CamNum, imgBuf, imgSize) was reading signed short instead of unsigned short.
But imgBuf has been defined with the correct Image_type:

long imgSize = width*height*(1+(Image_type==ASI_IMG_RAW16));
unsigned char* imgBuf = new unsigned char[imgSize];

Is there a way to actually get the unsigned short?

Thank you,

Remi

code:

int main(int argc,char **argv)
{
void parse_args(int, char**);
int width, height;
int i;
char c;
bool bresult;

fitsfile *fptr; /* pointer to the FITS file; defined in fitsio.h */
int fitstatus = 0, ii, jj;
long fpixel = 1, naxis = 2, nelements, exposure;
long naxes[2] = {5496, 3672}; /* image is 5496 pixels wide by 3672 rows */
unsigned short array[3672][5496]:

int time1,time2;
int count=0;

int CamNum=0;
exptime = -1;

/*
* Read command line arguments
*/

parse_args(argc, argv);

int numDevices = ASIGetNumOfConnectedCameras();
if(numDevices <= 0)
{
printf("no camera connected, press any key to exit\n");
getchar();
return -1;
}
else {
ASI_CAMERA_INFO ASICameraInfo;
ASIGetCameraProperty(&ASICameraInfo, 0);
printf("0 %s\n", ASICameraInfo.Name);

if(ASIOpenCamera(CamNum) != ASI_SUCCESS)
{
printf("OpenCamera error,are you root?,press any key to exit\n");
getchar();
return -1;
}
ASIInitCamera(CamNum);
ASI_ERROR_CODE err = ASIEnableDarkSubtract(CamNum, "dark.bmp");
if(err == ASI_SUCCESS)
printf("load dark ok\n");
else
printf("load dark failed %d\n", err);

// printf("%s information\n",ASICameraInfo.Name);
int iMaxWidth, iMaxHeight;
iMaxWidth = ASICameraInfo.MaxWidth;
iMaxHeight = ASICameraInfo.MaxHeight;
// printf("resolution:%d X %d\n", iMaxWidth, iMaxHeight);

/* ASI_CONTROL_CAPS ControlCaps;
int iNumOfCtrl = 0;
ASIGetNumOfControls(CamNum, &iNumOfCtrl);
for( i = 0; i < iNumOfCtrl; i++)
{
ASIGetControlCaps(CamNum, i, &ControlCaps);
printf("%s %s %f\n", ControlCaps.Name, ControlCaps.Description, ControlCaps.DefaultValue);
}
*/
long ltemp = 0;
ASI_BOOL bAuto = ASI_FALSE;
err = ASISetControlValue(CamNum, ASI_GAIN, 50, ASI_FALSE);
err = ASISetControlValue(CamNum, ASI_TARGET_TEMP, 0, ASI_FALSE);
err = ASISetControlValue(CamNum, ASI_COOLER_ON, 1, ASI_TRUE);
err = ASIGetControlValue(CamNum, ASI_TEMPERATURE, &ltemp, &bAuto);
printf("sensor temperature:%02f\n", (float)ltemp/10.0);
while (ltemp > 0.1) {
usleep(30000000);
ASIGetControlValue(CamNum, ASI_TEMPERATURE, &ltemp, &bAuto);
printf("sensor temperature:%02f\n", (float)ltemp/10.0);
}
// Set image caracteristics: width, height, binning, type (ASI_IMG_RAW8 or ASI_IMG_RAW16)
// default values are CCD size 5496x3672, binning 1, TYPE 16.
width = iMaxWidth;
height = iMaxHeight;
printf("width=%d height=%d\n",iMaxWidth,iMaxHeight);
int bin = 1;

ASI_IMG_TYPE Image_type = ASI_IMG_RAW16;

err = ASISetROIFormat(CamNum, width, height, bin, Image_type);
if (err == ASI_SUCCESS)
printf("Set ROI ok: width %d height %d bin %d Image_type %d\n",
width, height, bin,Image_type);
else
printf("Set ROI failed %d\n", err);

usleep(10000);//10ms


long imgSize = width*height*(1+(Image_type==ASI_IMG_RAW16));
unsigned char* imgBuf = new unsigned char[imgSize];

//ASISetControlValue(CamNum, ASI_GAIN, 0, ASI_FALSE);

int exp_ms; // input exposure time(ms)

if (exptime<0)
exp_ms = 10000;
else
exp_ms = 1000*exptime;

ASISetControlValue(CamNum, ASI_EXPOSURE, exp_ms*1000, ASI_FALSE);
ASISetControlValue(CamNum, ASI_BANDWIDTHOVERLOAD, 40, ASI_FALSE);

time1 = GetTickCount();

ASI_EXPOSURE_STATUS status;

int iDropped = 0;
//bool bSave = true;
ASIStartExposure(CamNum, ASI_FALSE);
usleep(1000000);//1000ms
status = ASI_EXP_WORKING;
while(status == ASI_EXP_WORKING)
{
ASIGetExpStatus(CamNum, &status);
}
if(status == ASI_EXP_SUCCESS)
{
ASIGetDataAfterExp(CamNum, imgBuf, imgSize);
// printf("imgBuf ");
// for (i=0;i<10;i++) {
// printf(" %d ", imgBuf);
// }
// printf("\n");
time2 = GetTickCount();
count++;

if(time2-time1 > 1000 )
{
ASIGetDroppedFrames(CamNum, &iDropped);

count = 0;
time1=GetTickCount();
printf("fps:%d dropped frames:%d ImageType:%d\n",count, iDropped, Image_type);


}

// Save a fits image in a folder of the night and time

istimeto();
sprintf(commande,"/bin/mkdir -p ./TBLAstrometry/data/%s", nuit);
system(commande);

sprintf(fitsname,"./TBLAstrometry/data/%s/%s",nuit,image_name);

// fits_create_img(fitsfile *fptr, int bitpix, int naxis, long *naxes, int *fitstatus)
// fits_write_pix(fitsfile *fptr, int datatype, long *fpixel, long nelements,
// void *array, int *fitstatus);

fits_create_file(&fptr, fitsname, &fitstatus);
fits_create_img(fptr, SHORT_IMG, naxis, naxes, &fitstatus); // SHORT_IMG = 16
exposure = exptime*1000;
fits_update_key(fptr, TLONG, "EXPOSURE", &exposure, "Total Exposure Time [ms]", &fitstatus);
nelements = naxes[0] * naxes[1]; /* number of pixels to write */
for (jj = 0; jj < naxes[1]; jj++) {
for (ii = 0; ii < naxes[0]; ii++) {
array[jj][ii] = imgBuf[ii+jj*5496];
}
}
fits_write_img(fptr, TUSHORT, fpixel, nelements, &array[0], &fitstatus);
//fits_write_img(fptr, TUSHORT, fpixel, nelements, &imgBuf[0], &fitstatus);
fits_close_file(fptr, &fitstatus); /* close the file */
fits_report_error(stderr, fitstatus);
}

ASIStopExposure(CamNum);
ASICloseCamera(CamNum);
if(imgBuf)
delete[] imgBuf;
printf("main function over\n");
return 1;
}
}

User avatar
chad
Posts: 635
Joined: Thu Feb 09, 2017 4:58 am

Re: RAW16 and ASI183

Post by chad » Tue Oct 22, 2019 3:26 am

Hi,
it should be the problem of saving.
For the fits file, there is a item named BZERO, if it is 0, the data of the image will be recoginzed as UN-signed. if not, it is signed.
For you code, we did not find this operation. So I think it should be a problem of saving the image to fits. I think you can check this part of code.
Thanks
Chad
ZWO Driver Engineer
Location:lon=120.6 lat=31.3
SuZhou China

rcabanac
Posts: 7
Joined: Thu Dec 07, 2017 5:44 pm

Re: RAW16 and ASI183

Post by rcabanac » Tue Oct 22, 2019 3:04 pm

Thanks Chad. I worked along your suggestion.

Note that BZERO =0 does not lead to a 0-65535 range, in my case, but to 0-32767, which tells me that ImgBuff is not coded as unsigned but as signed. Hence, setting BZERO to 32768 successfully solves the issue.

Cheers,

Remi

Attached is a working code.


int main(int argc,char **argv)
{
void parse_args(int, char**);
int width, height;
int i;
char c;
bool bresult;

fitsfile *fptr; /* pointer to the FITS file; defined in fitsio.h */
int fitstatus = 0, ii, jj;
long fpixel = 1, naxis = 2, nelements, exposure;
long naxes[2] = {5496, 3672}; /* image is 5496 pixels wide by 3672 rows */
// long naxes[2] = {1280, 960}; /* image is 1280 pixels wide by 960 rows */
long bzero, bscale, gain, xbin, ybin;
float pixsize;

int time1,time2;
int count=0;

int CamNum=0;
exptime = -1;

/*
* Read command line arguments
*/

parse_args(argc, argv);

int numDevices = ASIGetNumOfConnectedCameras();
if(numDevices <= 0)
{
printf("no camera connected, press any key to exit\n");
getchar();
return -1;
}
else {
ASI_CAMERA_INFO ASICameraInfo;
ASIGetCameraProperty(&ASICameraInfo, 0);
printf("0 %s\n", ASICameraInfo.Name);

if(ASIOpenCamera(CamNum) != ASI_SUCCESS)
{
printf("OpenCamera error,are you root?,press any key to exit\n");
getchar();
return -1;
}
ASIInitCamera(CamNum);
ASI_ERROR_CODE err = ASIEnableDarkSubtract(CamNum, "dark.bmp");
if(err == ASI_SUCCESS)
printf("load dark ok\n");
else
printf("load dark failed %d\n", err);

// printf("%s information\n",ASICameraInfo.Name);
int iMaxWidth, iMaxHeight;
iMaxWidth = ASICameraInfo.MaxWidth;
iMaxHeight = ASICameraInfo.MaxHeight;
// printf("resolution:%d X %d\n", iMaxWidth, iMaxHeight);

/* ASI_CONTROL_CAPS ControlCaps;
int iNumOfCtrl = 0;
ASIGetNumOfControls(CamNum, &iNumOfCtrl);
for( i = 0; i < iNumOfCtrl; i++)
{
ASIGetControlCaps(CamNum, i, &ControlCaps);
printf("%s %s %f\n", ControlCaps.Name, ControlCaps.Description, ControlCaps.DefaultValue);
}*/
// ASI183MM Pro
// Gain Gain 0.000000
// Exposure Exposure Time(us) 0.000000
// Offset offset 0.000000
// BandWidth The total data transfer rate percentage 0.000000
// Flip Flip: 0->None 1->Horiz 2->Vert 3->Both 0.000000
// AutoExpMaxGain Auto exposure maximum gain value 0.000000
// AutoExpMaxExpMS Auto exposure maximum exposure value(unit ms) 0.000000
// AutoExpTargetBrightness Auto exposure target brightness value 0.000000
// HardwareBin Is hardware bin2:0->No 1->Yes 0.000000
// HighSpeedMode Is high speed mode:0->No 1->Yes 0.000000
// Temperature Sensor temperature(degrees Celsius) 0.000000
// CoolPowerPerc Cooler power percent 0.000000
// TargetTemp Target temperature(cool camera only) 0.000000
// CoolerOn turn on/off cooler(cool camera only) 0.000000

long ltemp = 0;
ASI_BOOL bAuto = ASI_FALSE;
err = ASISetControlValue(CamNum, ASI_GAIN, 50, ASI_FALSE);
err = ASISetControlValue(CamNum, ASI_TARGET_TEMP, -1, ASI_FALSE);
err = ASISetControlValue(CamNum, ASI_COOLER_ON, 1, ASI_TRUE);
err = ASIGetControlValue(CamNum, ASI_TEMPERATURE, &ltemp, &bAuto);
printf("sensor temperature:%02f\n", (float)ltemp/10.0);
while (ltemp > -1) {
usleep(30000000);
ASIGetControlValue(CamNum, ASI_TEMPERATURE, &ltemp, &bAuto);
printf("sensor temperature:%02f\n", (float)ltemp/10.0);
}
// Set image caracteristics: width, height, binning, type (ASI_IMG_RAW8 or ASI_IMG_RAW16)
// default values are CCD size 5496x3672, binning 1, TYPE 16.
width = iMaxWidth;
height = iMaxHeight;
printf("width=%d height=%d\n",iMaxWidth,iMaxHeight);
int bin = 1;
ASI_IMG_TYPE Image_type = ASI_IMG_RAW16;

err = ASISetROIFormat(CamNum, width, height, bin, Image_type);
if (err == ASI_SUCCESS)
printf("Set ROI ok: width %d height %d bin %d Image_type %d\n",
width, height, bin,Image_type);
else
printf("Set ROI failed %d\n", err);

usleep(10000);//10ms


long imgSize = width*height*(1+(Image_type==ASI_IMG_RAW16));
unsigned char* imgBuf = new unsigned char[imgSize];

//ASISetControlValue(CamNum, ASI_GAIN, 0, ASI_FALSE);

int exp_ms; // input exposure time(ms)

if (exptime<0)
exp_ms = 10000;
else
exp_ms = 1000*exptime;

ASISetControlValue(CamNum, ASI_EXPOSURE, exp_ms*1000, ASI_FALSE);
ASISetControlValue(CamNum, ASI_BANDWIDTHOVERLOAD, 40, ASI_FALSE);

time1 = GetTickCount();

ASI_EXPOSURE_STATUS status;

int iDropped = 0;
//bool bSave = true;
ASIStartExposure(CamNum, ASI_FALSE);
usleep(1000000);//1000ms
status = ASI_EXP_WORKING;
while(status == ASI_EXP_WORKING)
{
ASIGetExpStatus(CamNum, &status);
}
if(status == ASI_EXP_SUCCESS)
{
ASIGetDataAfterExp(CamNum, imgBuf, imgSize);
printf("imgBuf ");
for (i=0;i<20;i+=2) {
printf(" %d ", 256*imgBuf+imgBuf[i+1]);
}
// printf("\n");
time2 = GetTickCount();
count++;

if(time2-time1 > 1000 )
{
ASIGetDroppedFrames(CamNum, &iDropped);

count = 0;
time1=GetTickCount();
printf("fps:%d dropped frames:%d ImageType:%d\n",count, iDropped, Image_type);


}

// Save a fits image in a folder of the night and time

istimeto();
sprintf(commande,"/bin/mkdir -p ./TBLAstrometry/data/%s", nuit);
system(commande);

sprintf(fitsname,"./TBLAstrometry/data/%s/%s",nuit,image_name);

// fits_create_img(fitsfile *fptr, int bitpix, int naxis, long *naxes, int *fitstatus)
// fits_write_pix(fitsfile *fptr, int datatype, long *fpixel, long nelements,
// void *array, int *fitstatus);
bzero = 32768;
bscale = 1;
gain = 50;
bin = 1;
pixsize = 2.4*bin;
fits_create_file(&fptr, fitsname, &fitstatus);
fits_create_img(fptr, SHORT_IMG, naxis, naxes, &fitstatus); // SHORT_IMG = 16
exposure = exptime*1000;
fits_update_key(fptr, TLONG, "EXPOSURE", &exposure, "Total Exposure Time [ms]", &fitstatus);
fits_update_key(fptr, TLONG, "BZERO", &bzero, "physical = BZERO + BSCALE*array_value", &fitstatus);
fits_update_key(fptr, TLONG, "BSCALE", &bscale, "physical = BZERO + BSCALE*array_value", &fitstatus);
fits_update_key(fptr, TLONG, "GAIN", &gain, "The ratio of output / input", &fitstatus);
fits_update_key(fptr, TLONG, "XBIN", &xbin, "Binning factor in width", &fitstatus);
fits_update_key(fptr, TLONG, "YBIN", &ybin, "Binning factor in height", &fitstatus);
fits_update_key(fptr, TFLOAT, "XPIXSZ", &pixsize, "Pixel Width in um (after binning)", &fitstatus);
fits_update_key(fptr, TFLOAT, "YPIXSZ", &pixsize, "Pixel height in um (after binning)", &fitstatus);

nelements = naxes[0] * naxes[1]; /* number of pixels to write */
fits_write_img(fptr, TUSHORT, fpixel, nelements, &imgBuf[0], &fitstatus);
fits_close_file(fptr, &fitstatus); /* close the file */
fits_report_error(stderr, fitstatus);
}

ASIStopExposure(CamNum);
ASICloseCamera(CamNum);
if(imgBuf)
delete[] imgBuf;
printf("main function over\n");
return 1;
}
}

User avatar
chad
Posts: 635
Joined: Thu Feb 09, 2017 4:58 am

Re: RAW16 and ASI183

Post by chad » Wed Oct 23, 2019 1:11 am

OK, It is fine to solve the problem. Fits's header is necessary.
Thanks
Chad
ZWO Driver Engineer
Location:lon=120.6 lat=31.3
SuZhou China

Post Reply