/********************************************************************************/ /* * File name: MDigGraBayerb.c * * Synopsis: The following example shows how to color balance an image from * a bayer camera. Depending on the lighting conditions of the * subject (i.e. color temperature), converted bayer images will * need to be color balanced or white balanced to accurately * reproduce color. MIL includes mechanisms to compute * the appropriate white balancing coefficients for converting a * raw bayer image into a color image. These mechanisms rely on using * a test image for computing the bayer coefficients. Ideally, the * test image has a consistent neutral gray background and is free * of any saturated pixels. Saturated pixels in a test image will * always lead to an invalid color correction. * Please consult "Correcting the white balance of your Bayer images" * subsection in the MIL user guide. */ #include #include void OverlayDraw(MIL_ID MilDisplay, long *SizeX, long *SizeY, long *OffsetX, long *OffsetY); void main(void) { MIL_ID MilApplication, /* Application identifier. */ MilSystem, /* System identifier. */ MilDisplay, /* Display identifier. */ MilDigitizer, /* Digitizer identifier. */ MilRawBayer, /* Buffer used to grab raw bayer data and from which to extract white balancing information. */ MilRawChil, /* Region of raw buffer used for white balancing. */ MilWBCoeff, /* Buffer in which the white balancing coefficients will be stored. */ MilImage; /* Image buffer identifier. */ long BayerPattern, /* Bayer pattern set in the DCF. */ SizeX, SizeY, OffsetX, OffsetY; float Coefficients[3] = {0.0}; /* Allocate defaults. */ MappAllocDefault(M_SETUP, &MilApplication, &MilSystem, &MilDisplay, &MilDigitizer, &MilImage); /* Verify that the DCF is bayer. */ MdigInquire(MilDigitizer, M_BAYER_PATTERN, &BayerPattern); if(BayerPattern == M_NULL) { printf("You must have a bayer camera and a bayer enabled DCF to run this example\n"); MappFreeDefault(MilApplication, MilSystem, MilDisplay, MilDigitizer, MilImage); return; } printf("This example illustrates how to color balance an image from a bayer camera.\n"); printf("\nPress to continue\n\n"); getch(); MbufAlloc2d(MilSystem, (long)MdigInquire(MilDigitizer, M_SIZE_X, M_NULL), (long)MdigInquire(MilDigitizer, M_SIZE_Y, M_NULL), 8+M_UNSIGNED, M_IMAGE+M_GRAB+M_PROC, &MilRawBayer); printf("Grabbing and converting to color without color balancing.\n"); printf("Press to enable automatic white balancing.\n\n"); MdigGrabContinuous(MilDigitizer, MilImage); getch(); MdigHalt(MilDigitizer); /* Enable automatic white balancing. This control calculates white balancing coefficients that will be used to perform color correction on the color-converted bayer image. The coefficients are calculated from a grabbed image and the entire frame is used to perform the calculations. */ MdigControl(MilDigitizer, M_WHITE_BALANCE, M_CALCULATE); /* Retrieve the calculated coefficients. */ MdigInquire(MilDigitizer, M_BAYER_COEFFICIENTS_ID, &MilWBCoeff); MbufGet(MilWBCoeff, Coefficients); printf("Automatic white balancing enabled\n"); printf("The coefficients have been calculated using the entire image.\n"); printf("Red: %f ", Coefficients[0]); printf("Green: %f ", Coefficients[1]); printf("Blue: %f ", Coefficients[2]); printf("\n\nPress to continue\n\n"); /* Grab continuously. */ MdigGrabContinuous(MilDigitizer, MilImage); getch(); /* Highlight a rectangular region that will be used to calculate the white balancing coefficients */ OverlayDraw(MilDisplay, &SizeX, &SizeY, &OffsetX, &OffsetY); printf("Please place a neutral gray object filling the entire rectangle.\n"); printf("Press to perform white balancing using this part of the image.\n\n"); getch(); /* Stop displaying the rectangle. */ MdispControl(MilDisplay, M_OVERLAY_SHOW, M_DISABLE); /* Stop the continuous grab. */ MdigHalt(MilDigitizer); /* Grab a raw bayer image. */ MdigControl(MilDigitizer, M_BAYER_CONVERSION, M_DISABLE); MdigGrab(MilDigitizer, MilRawBayer); /* Using the rectangular region, compute the new white balancing coefficients. */ MbufChild2d(MilRawBayer, OffsetX, OffsetY, SizeX, SizeY, &MilRawChil); MbufBayer(MilRawChil, MilImage, MilWBCoeff, BayerPattern+M_WHITE_BALANCE_CALCULATE); MbufClear(MilImage, 0); printf("The newly calculated white balancing coefficients are:\n"); MbufGet(MilWBCoeff, Coefficients); printf("Red: %f ", Coefficients[0]); printf("Green: %f ", Coefficients[1]); printf("Blue: %f ", Coefficients[2]); /* Re-enable the bayer conversion. */ MdigControl(MilDigitizer, M_BAYER_CONVERSION, M_ENABLE); /* Store the new coefficients for the digitizers to use. */ MdigControl(MilDigitizer, M_BAYER_COEFFICIENTS_ID, MilWBCoeff); /* Resume grabbing. */ MdigGrabContinuous(MilDigitizer, MilImage); /* Pause to show the result. */ printf("\n\nPress to stop.\n\n"); getch(); /* Pause to show the result. */ printf("Displaying the last image.\n"); printf("Press to end.\n\n"); getch(); MdigHalt(MilDigitizer); MbufFree(MilRawChil); MbufFree(MilRawBayer); MbufFree(MilWBCoeff); /* Free defaults. */ MappFreeDefault(MilApplication, MilSystem, MilDisplay, MilDigitizer, MilImage); } /* --------------------------------------------------------------- */ /* Name: OverlayDraw * Synopsis: This function draws annotations in the display overlay. */ void OverlayDraw(MIL_ID MilDisplay, long *SizeX, long *SizeY, long *OffsetX, long *OffsetY) { MIL_ID MilOverlayImage; long ImageWidth, ImageHeight; /* Prepare overlay buffer. */ /***************************/ /* Enable display overlay annotations. */ MdispControl(MilDisplay, M_OVERLAY, M_ENABLE); /* Inquire the overlay buffer associated with the display. */ MdispInquire(MilDisplay, M_OVERLAY_ID, &MilOverlayImage); /* Clear the overlay to transparent. */ MdispControl(MilDisplay, M_OVERLAY_CLEAR, M_DEFAULT); /* Disable the overlay display updating to accelerate annotations. */ MdispControl(MilDisplay, M_OVERLAY_SHOW, M_DISABLE); /* Inquire overlay size. */ ImageWidth = MbufInquire(MilOverlayImage, M_SIZE_X, M_NULL); ImageHeight = MbufInquire(MilOverlayImage, M_SIZE_Y, M_NULL); *SizeX = ImageWidth/5; *SizeY = ImageHeight/5; *OffsetX = ImageWidth/2 - (*SizeX/2); *OffsetY = ImageHeight/2 - (*SizeY/2); if((*OffsetX) & 0x1) (*OffsetX)++; if((*OffsetY) & 0x1) (*OffsetY)++; /* Draw MIL overlay annotations. */ /*********************************/ /* Set graphic text to transparent background. */ MgraControl(M_DEFAULT, M_BACKGROUND_MODE, M_TRANSPARENT); /* Print a green string in the overlay image buffer. */ MgraColor(M_DEFAULT, M_COLOR_RED); MgraRect(M_DEFAULT, MilOverlayImage, *OffsetX, *OffsetY, (*OffsetX)+(*SizeX), (*OffsetY)+(*SizeY)); /* Re-enable the overlay display after all annotations are done. */ MdispControl(MilDisplay, M_OVERLAY_SHOW, M_ENABLE); }