📄 image_c_opencv.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 <stdio.h>
#include <opencv/highgui.h>
/* 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, &minVal, &maxVal, &minLoc, &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 < image->height; i++)
for (j = 0; j < image->width; j++)
cvSetReal2D(image, i, j, *(data_p++));
}
void char_to_image(char *data, IplImage *image)
{
int i, j;
for (i = 0; i < image->height; i++)
{
for (j = 0; j < image->width; j++)
{
image->imageData[i*image->widthStep + j*3] =
(char)data[i*image->width*3*1 + j*3];
image->imageData[i*image->widthStep + j*3 + 1] =
(char)data[i*image->width*3*1 + j*3 + 1];
image->imageData[i*image->widthStep + j*3 + 2] =
(char)data[i*image->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 < image->height; i++)
for (j = 0; j < image->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 < image->height; i++)
for (j = 0; j < image->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->width, image->height),
IPL_DEPTH_8U, 1);
cvMinMaxLoc(image, &minVal, &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, &minVal, &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(&temp);
}
void save_scaled(char *name, IplImage *image)
{
double minVal, maxVal;
IplImage *temp;
temp = cvCreateImage(cvSize(image->width, image->height),
IPL_DEPTH_8U, 1);
cvMinMaxLoc(image, &minVal, &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, &minVal, &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(&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, &minVal, &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, &minVal, &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 + -