⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 image_c_opencv.htm

📁 When I use opencv, I use this very useful paper to begin the study. This is all I searched from the
💻 HTM
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0039)http://www.timrost.com/msc_code/image.c -->
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=gb2312">
<META content="MSHTML 6.00.2900.3020" name=GENERATOR></HEAD>
<BODY><PRE>#include "image.h"
#include "global.h"
#include &lt;stdio.h&gt;
#include &lt;opencv/highgui.h&gt;

/* Debugging function.  For the given array pointer
 * and name, print the minimum and maximum values
 * present in the data structure.
 */

void print_array_minmax(char *name, CvArr *image)
{
	double minVal, maxVal;
	CvPoint minLoc, maxLoc;

	cvMinMaxLoc(image, &amp;minVal, &amp;maxVal, &amp;minLoc, &amp;maxLoc, 0);
	printf("%s: ", name);
	printf("minimum value = %f; maximum value = %f\n", minVal, maxVal);
}

void double_to_image(double *data, IplImage *image)
{
	int i, j;
	double *data_p = data;

	for (i = 0; i &lt; image-&gt;height; i++)
	for (j = 0; j &lt; image-&gt;width; j++)
		cvSetReal2D(image, i, j, *(data_p++));
}

void char_to_image(char *data, IplImage *image)
{
	int i, j;
	for (i = 0; i &lt; image-&gt;height; i++)
	{
		for (j = 0; j &lt; image-&gt;width; j++)
		{
			image-&gt;imageData[i*image-&gt;widthStep + j*3] =
                    (char)data[i*image-&gt;width*3*1 + j*3];
                image-&gt;imageData[i*image-&gt;widthStep + j*3 + 1] =
                    (char)data[i*image-&gt;width*3*1 + j*3 + 1];
                image-&gt;imageData[i*image-&gt;widthStep + j*3 + 2] =
                    (char)data[i*image-&gt;width*3*1 + j*3 + 2];
		}
	}
}

void image_to_char(IplImage *image, char *data)
{
	int i, j;
	char *data_ptr = data;

	for (i = 0; i &lt; image-&gt;height; i++)
	for (j = 0; j &lt; image-&gt;width; j++)
		*(data_ptr++) = cvGetReal2D(image, i, j);
}

void image_to_double(IplImage *image, double *data)
{
	int i, j;
	double *data_ptr = data;

	for (i = 0; i &lt; image-&gt;height; i++)
	for (j = 0; j &lt; image-&gt;width; j++)
		*(data_ptr++) = cvGetReal2D(image, i, j);
}

/* For the given image pointer scale to 8-bit
 * values and display in a window.  Make necessary
 * type conversion to 8-bit unsigned.
 */

void display_scaled(char *name, IplImage *image)
{
	double minVal, maxVal;
	IplImage *temp;
	temp = cvCreateImage(cvSize(image-&gt;width, image-&gt;height),
				IPL_DEPTH_8U, 1);

	cvMinMaxLoc(image, &amp;minVal, &amp;maxVal, NULL, NULL, 0);

	// Scale the image values for a maximum of 255.
	// cvShowImage only works correctly for 8-bit
	// unsigned data so this function also ensures
	// the displayed image is IPL_DEPTH_8U

	// Avoid divide by zero errors
	if (maxVal != 0)
		cvConvertScale(image, temp, 255.0/maxVal, 0);
	else
	{
		printf("display_scaled warning: max image pixel value is zero.\n");
		cvConvertScale(image, temp, 255.0, 0);
	}

	// Check to ensure the scaling functioned correct.
	// Display an error message if it went awry.
	cvMinMaxLoc(temp, &amp;minVal, &amp;maxVal, NULL, NULL, 0);
	if (minVal != 0)
		fprintf(stderr, "Error: %s minVal is %f instead of 0\n",
						name, minVal);
	if (maxVal != 255)
		fprintf(stderr, "Error: %s maxVal is %f instead of 255\n",
						name, maxVal);

	cvNamedWindow(name, -1);
	cvShowImage(name, temp);

	cvReleaseImage(&amp;temp);
}

void save_scaled(char *name, IplImage *image)
{
	double minVal, maxVal;
	IplImage *temp;
	temp = cvCreateImage(cvSize(image-&gt;width, image-&gt;height),
				IPL_DEPTH_8U, 1);

	cvMinMaxLoc(image, &amp;minVal, &amp;maxVal, NULL, NULL, 0);

	// Scale the image values for a maximum of 255.
	// cvShowImage only works correctly for 8-bit
	// unsigned data so this function also ensures
	// the displayed image is IPL_DEPTH_8U

	// Avoid divide by zero errors
	if (maxVal != 0)
		cvConvertScale(image, temp, 255.0/maxVal, 0);
	else
	{
		printf("display_scaled warning: max image pixel value is zero.\n");
		cvConvertScale(image, temp, 255.0, 0);
	}

	// Check to ensure the scaling functioned correct.
	// Display an error message if it went awry.
	cvMinMaxLoc(temp, &amp;minVal, &amp;maxVal, NULL, NULL, 0);
	if (minVal != 0)
		fprintf(stderr, "Error: %s minVal is %f instead of 0\n",
						name, minVal);
	if (maxVal != 255)
		fprintf(stderr, "Error: %s maxVal is %f instead of 255\n",
						name, maxVal);

	cvSaveImage(name, temp);
	cvReleaseImage(&amp;temp);
}

/* For the given image pointer scale to 8-bit
 * values.  Make necessary type conversion
 * to 8-bit unsigned.
 */

void image_scale(IplImage *source, IplImage *dest)
{
	double minVal, maxVal;

	cvMinMaxLoc(source, &amp;minVal, &amp;maxVal, NULL, NULL, 0);

	// Scale the image values for a maximum of 255.
	// cvShowImage only works correctly for 8-bit
	// unsigned data so this function also ensures
	// the displayed image is IPL_DEPTH_8U

	// Avoid divide by zero errors
	if (maxVal != 0)
		cvConvertScale(source, dest, 255.0/maxVal, 0);
	else
	{
		printf("display_scaled warning: max image pixel value is zero.\n");
		cvConvertScale(source, dest, 255.0, 0);
	}

	// Check to ensure the scaling functioned correct.
	// Display an error message if it went awry.
	cvMinMaxLoc(dest, &amp;minVal, &amp;maxVal, NULL, NULL, 0);
	if (minVal != 0)
		fprintf(stderr, "Error: minVal is %f instead of 0\n", minVal);
	if (maxVal != 255)
		fprintf(stderr, "Error: maxVal is %f instead of 255\n", maxVal);
}

</PRE></BODY></HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -