bool Calibration::findBoard(Mat img, vector<Point2f>& pointBuf, bool refine) {
        bool found=false;
        if(patternType == CHESSBOARD) {
            // no CV_CALIB_CB_FAST_CHECK, because it breaks on dark images (e.g., dark IR images from kinect)
            int chessFlags = CV_CALIB_CB_ADAPTIVE_THRESH;// | CV_CALIB_CB_NORMALIZE_IMAGE;
            found = findChessboardCorners(img, patternSize, pointBuf, chessFlags);
            
            // improve corner accuracy
            if(found) {
                if(img.type() != CV_8UC1) {
                    copyGray(img, grayMat);
                } else {
                    grayMat = img;
                }
                
                if(refine) {
                    // the 11x11 dictates the smallest image space square size allowed
                    // in other words, if your smallest square is 11x11 pixels, then set this to 11x11
                    cornerSubPix(grayMat, pointBuf, subpixelSize,  cv::Size(-1,-1), TermCriteria(CV_TERMCRIT_EPS + CV_TERMCRIT_ITER, 30, 0.1 ));
                }
            }
        }
#ifdef USING_OPENCV_2_3
        else {
            int flags = (patternType == CIRCLES_GRID ? CALIB_CB_SYMMETRIC_GRID : CALIB_CB_ASYMMETRIC_GRID); // + CALIB_CB_CLUSTERING
            found = findCirclesGrid(img, patternSize, pointBuf, flags);
        }
#endif
        return found;
    }
	void ObjectFinder::update(cv::Mat img) {
        cv::Mat gray;
        if(getChannels(img) == 1) gray = img;
        else                      copyGray(img,gray);
        resize(gray, graySmall, rescale, rescale);
		cv::Mat graySmallMat = toCv(graySmall);
		if(useHistogramEqualization) {
			equalizeHist(graySmallMat, graySmallMat);
		}
		cv::Size minSize, maxSize;
		float averageSide = (graySmallMat.rows + graySmallMat.cols) / 2;
		if(minSizeScale > 0) {
			int side = minSizeScale * averageSide;
			minSize = cv::Size(side, side);
		}
		if(maxSizeScale < 1) {
			int side = maxSizeScale * averageSide;
			maxSize = cv::Size(side, side);
		}
		classifier.detectMultiScale(graySmallMat, objects, multiScaleFactor, minNeighbors,
																(cannyPruning ? CASCADE_DO_CANNY_PRUNING : 0) |
																(findBiggestObject ? CASCADE_FIND_BIGGEST_OBJECT | CASCADE_DO_ROUGH_SEARCH : 0),
																minSize,
																maxSize);
		for(int i = 0; i < objects.size(); i++) {
			cv::Rect& rect = objects[i];
			rect.width /= rescale, rect.height /= rescale;
			rect.x /= rescale, rect.y /= rescale;
		}
		tracker.track(objects);
	}
void frameGrabber::getImage(ImageIDL& img)
	{
	if (isOpen())
		{
		img.grayImage.dataValid = false;
		img.rgbImage.dataValid = false;
		img.hsvImage.dataValid = false;
		grabImage(img);
		switch (img.sourceFormat)
			{
			case kRGB:
				copyRGB(img);
				break;
			case kGray:
				copyGray(img);
				break;
			default:
				throw Miro::EOutOfBounds();
				break;
			}
		}
	else
		throw Miro::EDevIO();
	}
Example #4
0
void * PNGImageIO_writePNGData(BitmapImage * image, int pixelFormat, bool flipVertical, size_t * outLength) {
	png_structp pngWriteStruct;
	png_infop pngInfoStruct;
	png_byte ** rows = NULL;
	struct memwriteContext writeContext;
	int colorType;
	enum BitmapPixelFormat outputPixelFormat;
	unsigned char * transformedPixels = NULL, * pixels;
	unsigned int rowIndex;
	
	pngWriteStruct = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
	pngInfoStruct = png_create_info_struct(pngWriteStruct);
	
	if (setjmp(png_jmpbuf(pngWriteStruct))) {
		png_destroy_write_struct(&pngWriteStruct, &pngInfoStruct);
		free(rows);
		return NULL;
	}
	
	switch (image->pixelFormat) {
		case BITMAP_PIXEL_FORMAT_RGBA_8888:
			colorType = PNG_COLOR_TYPE_RGB_ALPHA;
			break;
			
		case BITMAP_PIXEL_FORMAT_RGB_888:
			colorType = PNG_COLOR_TYPE_RGB;
			break;
			
		case BITMAP_PIXEL_FORMAT_GRAYALPHA_88:
			colorType = PNG_COLOR_TYPE_GRAY_ALPHA;
			break;
			
		case BITMAP_PIXEL_FORMAT_GRAY_8:
			colorType = PNG_COLOR_TYPE_GRAY;
			break;
			
		default:
			png_destroy_write_struct(&pngWriteStruct, &pngInfoStruct);
			return NULL;
	}
	
	switch (pixelFormat) {
		case PNG_PIXEL_FORMAT_AUTOMATIC:
			outputPixelFormat = image->pixelFormat;
			break;
			
		case BITMAP_PIXEL_FORMAT_RGBA_8888:
			if ((int) image->pixelFormat != pixelFormat) {
				transformedPixels = malloc(image->width * image->height * 4);
				if (colorType & PNG_COLOR_MASK_ALPHA) {
					copyAlpha(transformedPixels, image->pixels, image->width, image->height, 4, BitmapImage_pixelFormatBytes(image->pixelFormat));
				} else {
					addAlphaByte(transformedPixels, image->width, image->height, 4);
				}
				if (colorType & PNG_COLOR_MASK_COLOR) {
					copyRGB(transformedPixels, image->pixels, image->width, image->height, 4, BitmapImage_pixelFormatBytes(image->pixelFormat));
				} else {
					copyGrayToRGB(transformedPixels, image->pixels, image->width, image->height, 4, BitmapImage_pixelFormatBytes(image->pixelFormat));
				}
			}
			colorType = PNG_COLOR_TYPE_RGB_ALPHA;
			outputPixelFormat = BITMAP_PIXEL_FORMAT_RGBA_8888;
			break;
			
		case BITMAP_PIXEL_FORMAT_RGB_888:
			if ((int) image->pixelFormat != pixelFormat) {
				transformedPixels = malloc(image->width * image->height * 3);
				if (colorType & PNG_COLOR_MASK_COLOR) {
					copyRGB(transformedPixels, image->pixels, image->width, image->height, 3, BitmapImage_pixelFormatBytes(image->pixelFormat));
				} else {
					copyGrayToRGB(transformedPixels, image->pixels, image->width, image->height, 3, BitmapImage_pixelFormatBytes(image->pixelFormat));
				}
			}
			colorType = PNG_COLOR_TYPE_RGB;
			outputPixelFormat = BITMAP_PIXEL_FORMAT_RGB_888;
			break;
			
		case BITMAP_PIXEL_FORMAT_GRAYALPHA_88:
			if ((int) image->pixelFormat != pixelFormat) {
				transformedPixels = malloc(image->width * image->height * 2);
				if (colorType & PNG_COLOR_MASK_ALPHA) {
					copyAlpha(transformedPixels, image->pixels, image->width, image->height, 2, BitmapImage_pixelFormatBytes(image->pixelFormat));
				} else {
					addAlphaByte(transformedPixels, image->width, image->height, 2);
				}
				if (colorType & PNG_COLOR_MASK_COLOR) {
					copyRGBToGray(transformedPixels, image->pixels, image->width, image->height, 2, BitmapImage_pixelFormatBytes(image->pixelFormat));
				} else {
					copyGray(transformedPixels, image->pixels, image->width, image->height, 2, BitmapImage_pixelFormatBytes(image->pixelFormat));
				}
			}
			colorType = PNG_COLOR_TYPE_GRAY_ALPHA;
			outputPixelFormat = BITMAP_PIXEL_FORMAT_GRAYALPHA_88;
			break;
			
		case BITMAP_PIXEL_FORMAT_GRAY_8:
			if ((int) image->pixelFormat != pixelFormat) {
				transformedPixels = malloc(image->width * image->height);
				if (colorType & PNG_COLOR_MASK_COLOR) {
					copyRGBToGray(transformedPixels, image->pixels, image->width, image->height, 1, BitmapImage_pixelFormatBytes(image->pixelFormat));
				} else {
					copyGray(transformedPixels, image->pixels, image->width, image->height, 1, BitmapImage_pixelFormatBytes(image->pixelFormat));
				}
			}
			colorType = PNG_COLOR_TYPE_GRAY;
			outputPixelFormat = BITMAP_PIXEL_FORMAT_GRAY_8;
			break;
			
		default:
			png_destroy_write_struct(&pngWriteStruct, &pngInfoStruct);
			return NULL;
	}
	
	writeContext = memwriteContextInit(malloc(1), 0, 1, true);
	png_set_write_fn(pngWriteStruct, &writeContext, pngWriteFnMemwrite, pngFlushFnNoOp);
	
	png_set_IHDR(pngWriteStruct, pngInfoStruct, image->width, image->height, 8, colorType, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
	
	rows = malloc(sizeof(png_byte *) * image->height);
	pixels = transformedPixels == NULL ? image->pixels : transformedPixels;
	for (rowIndex = 0; rowIndex < image->height; rowIndex++) {
		rows[rowIndex] = pixels + ((flipVertical ? image->height - rowIndex - 1 : rowIndex) * image->width * BitmapImage_pixelFormatBytes(outputPixelFormat));
	}
	
	png_set_rows(pngWriteStruct, pngInfoStruct, rows);
	png_write_png(pngWriteStruct, pngInfoStruct, PNG_TRANSFORM_IDENTITY, NULL);
	
	free(rows);
	free(transformedPixels);
	png_destroy_write_struct(&pngWriteStruct, &pngInfoStruct);
	
	*outLength = writeContext.position;
	return writeContext.data;
}