Commit 54bd2585 张士柳

1 个父辈 bebc2453
......@@ -597,9 +597,9 @@ namespace eyemLib_Sharp
//图像颜色空间转换
[DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)]
private static extern int eyemCvtImageColor(EyemImage tpImage, int iCCodes, ref EyemImage tpDstImg);
//图像数据格式转换
//图像数据格式转换(uint8_t、int8_t、uint16_t、int16_t、int32_t、float_t、double_t)
[DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)]
private static extern int eyemCvtImageType(EyemImage tpImage, int rType, double alpha, double beta, ref EyemImage tpDstImg);
private static extern int eyemCvtImageType(EyemImage tpImage, string ccSubType, double alpha, double beta, ref EyemImage tpDstImg);
//图像相加
[DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)]
private static extern int eyemImageAdd(EyemImage tpImage1, EyemImage tpImage2, ref EyemImage tpDstImg);
......@@ -704,6 +704,9 @@ namespace eyemLib_Sharp
public static void eyemReadImageTool(string fileName)
{
Stopwatch sw = new Stopwatch();
sw.Restart();
EyemImage image;
EyemImage tpDstImg = new EyemImage();
//int flag = eyemImageRead("D:\\图片及统计结果\\图片及统计结果\\data\\6D551\\6D551-R014212020040601587-20200624140637.png", 0, out ucpImage);
......@@ -715,35 +718,21 @@ namespace eyemLib_Sharp
return;
}
EyemImage image1 = new EyemImage();
EyemImage image2 = new EyemImage();
flag = eyemImageMalloc(image.iWidth, image.iHeight, 1, "int8_t", out image1);
//flag = eyemImageMalloc(image.iWidth, image.iHeight, 1, "uint16_t", out image1);
//flag = eyemImageMalloc(image.iWidth, image.iHeight, 1, "int8_t", out image2);
//flag = eyemImageAdd(image1, image2, ref tpDstImg);
//flag = eyemImageSub(image1, image2, ref tpDstImg)
//flag = eyemCvtImageColor(image, (int)ColorConversionCodes.COLOR_BGR2RGB, ref tpDstImg);
//flag = eyemImageAbs(image1, ref tpDstImg);
//flag = eyemCvtImageType(image2, "uint16_t", 1.0, 128, ref tpDstImg);
//flag = eyemCvtImageColor(ucpImage, (int)ColorConversionCodes.COLOR_BGR2RGB, ref tpDstImg);
//flag = eyemImageSub(image1, image2, ref tpDstImg);
//flag = eyemImageAbs(image1, ref tpDstImg);
//Bitmap bmp = new Bitmap(ucpImage.iWidth, ucpImage.iHeight, System.Drawing.Imaging.PixelFormat.Format8bppIndexed);
//ColorPalette color_palette_gray_ = bmp.Palette;
//for (int i = 0; i < 256; i++)
//{
// color_palette_gray_.Entries[i] = System.Drawing.Color.FromArgb(i, i, i);
//}
//bmp.Palette = color_palette_gray_;
//BitmapData bdd = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.WriteOnly, bmp.PixelFormat);
//memcpy((byte*)bdd.Scan0.ToPointer(), (byte*)ucpImage.vpImage.ToPointer(), bdd.Stride * bdd.Height);
//bmp.UnlockBits(bdd);
//bmp.Dispose();
Stopwatch sw = new Stopwatch();
sw.Restart();
#region Test Blob
//int ipNum;
......@@ -912,6 +901,80 @@ namespace eyemLib_Sharp
eyemImageFree(image.vpImage);
}
public static void eyemTest(string[] fileNames)
{
EyemImage imageFormer = new EyemImage();
EyemImage imageNext = new EyemImage();
EyemImage imageDiff = new EyemImage();
//首次需要为图像申请内存,EyemImage之间直接用=赋值是不行的
int flag = eyemImageMalloc(1080, 1440, 3, "uint8_t", out imageNext);
flag = eyemImageMalloc(1080, 1440, 3, "uint8_t", out imageFormer);
flag = eyemImageMalloc(1080, 1440, 3, "uint8_t", out imageDiff);
int nFrmNum = 0;
for (int i = 0; i < fileNames.Length; i++)
{
flag = eyemImageRead(fileNames[i], -1, out imageNext);
if (flag != 0)
{
Console.WriteLine("读图失败!");
return;
}
nFrmNum++;
if (nFrmNum > 1)
{
flag = eyemImageSub(imageNext, imageFormer, ref imageDiff);
}
//前一帧,(直接赋值是不对的)
imageFormer = imageNext;
}
eyemImageDispose(imageFormer);
eyemImageDispose(imageNext);
eyemImageDispose(imageDiff);
}
public static void eyemImageDispose(EyemImage tpImage)
{
eyemImageFree(tpImage.vpImage);
}
public static Bitmap eyemCvtToBitmap(EyemImage tpImage)
{
PixelFormat pixelFormat;
switch (tpImage.iChannels)
{
case 1:
pixelFormat = PixelFormat.Format16bppGrayScale;
break;
case 3:
pixelFormat = PixelFormat.Format24bppRgb;
break;
case 4:
pixelFormat = PixelFormat.Format32bppRgb;
break;
default:
//不支持通道数
return null;
}
int pixelPerByte = 1;
Bitmap bitmap = new Bitmap(tpImage.iWidth, tpImage.iHeight, tpImage.iWidth * tpImage.iChannels * pixelPerByte, pixelFormat, tpImage.vpImage);
if (pixelFormat == PixelFormat.Format8bppIndexed)
{
ColorPalette colorPalette = bitmap.Palette;
for (int i = 0; i < 256; i++)
{
colorPalette.Entries[i] = Color.FromArgb(i, i, i);
}
bitmap.Palette = colorPalette;
}
bitmap.Save("uint16_t_c1.bmp");
return bitmap;
}
#region 结构转内存指针
public static IntPtr teStructArray2IntPtr(EyemOcsDXY[] tpPoints, int iLength)
{
......
......@@ -21,12 +21,15 @@ namespace eyemLib_Sharp
// Console.Write("请按任意键继续。。。");
//}
foreach (var fileName in fileNames)
{
EyemLib.eyemReadImageTool(fileName);
Thread.Sleep(1);
break;
}
//foreach (var fileName in fileNames)
//{
// EyemLib.eyemReadImageTool(fileName);
// Thread.Sleep(1);
// break;
//}
EyemLib.eyemTest(fileNames);
Console.Write("请按任意键继续。。。");
Console.ReadKey();
}
......
......@@ -2,75 +2,78 @@
int eyemImageRead(const char *filename, int iFlag, EyemImage *tpImage)
{
cv::Mat src = cv::imread(filename, iFlag);
cv::Mat _src = cv::imread(filename, iFlag);
if (src.empty())
if (_src.empty())
return FUNC_IMAGE_NOT_EXIST;
//图像信息
tpImage->iWidth = src.cols; tpImage->iHeight = src.rows; tpImage->iDepth = src.depth(); tpImage->iChannels = src.channels();
tpImage->iWidth = _src.cols; tpImage->iHeight = _src.rows; tpImage->iDepth = _src.depth(); tpImage->iChannels = _src.channels();
//内存尺寸
int _Size = tpImage->iWidth*tpImage->iHeight*tpImage->iChannels;
switch (tpImage->iDepth)
{
case CV_8U:
_Size *= sizeof(uint8_t);
tpImage->vpImage = (uint8_t *)malloc(_Size);
if (NULL == tpImage->vpImage)
return FUNC_NOT_ENOUGH_MEM;
memset(tpImage->vpImage, 0, _Size);
break;
case CV_8S:
_Size *= sizeof(int8_t);
tpImage->vpImage = (int8_t *)malloc(_Size);
if (NULL == tpImage->vpImage)
return FUNC_NOT_ENOUGH_MEM;
memset(tpImage->vpImage, 0, _Size);
break;
case CV_16U:
_Size *= sizeof(uint16_t);
tpImage->vpImage = (uint16_t *)malloc(_Size);
if (NULL == tpImage->vpImage)
return FUNC_NOT_ENOUGH_MEM;
memset(tpImage->vpImage, 0, _Size);
break;
case CV_16S:
_Size *= sizeof(int16_t);
tpImage->vpImage = (int16_t *)malloc(_Size);
if (NULL == tpImage->vpImage)
return FUNC_NOT_ENOUGH_MEM;
memset(tpImage->vpImage, 0, _Size);
break;
case CV_32S:
_Size *= sizeof(int32_t);
tpImage->vpImage = (int32_t *)malloc(_Size);
if (NULL == tpImage->vpImage)
return FUNC_NOT_ENOUGH_MEM;
memset(tpImage->vpImage, 0, _Size);
break;
case CV_32F:
_Size *= sizeof(float_t);
tpImage->vpImage = (float_t *)malloc(_Size);
if (NULL == tpImage->vpImage)
return FUNC_NOT_ENOUGH_MEM;
memset(tpImage->vpImage, 0, _Size);
break;
case CV_64F:
_Size *= sizeof(double_t);
tpImage->vpImage = (double_t *)malloc(_Size);
if (NULL == tpImage->vpImage)
return FUNC_NOT_ENOUGH_MEM;
memset(tpImage->vpImage, 0, _Size);
break;
default:
//no support format
return FUNC_CANNOT_USE;
//判断是否需要重新申请内存
if (NULL == tpImage->vpImage) {
//需要申请内存
switch (tpImage->iDepth)
{
case CV_8U:
_Size *= sizeof(uint8_t);
tpImage->vpImage = (uint8_t *)malloc(_Size);
if (NULL == tpImage->vpImage)
return FUNC_NOT_ENOUGH_MEM;
memset(tpImage->vpImage, 0, _Size);
break;
case CV_8S:
_Size *= sizeof(int8_t);
tpImage->vpImage = (int8_t *)malloc(_Size);
if (NULL == tpImage->vpImage)
return FUNC_NOT_ENOUGH_MEM;
memset(tpImage->vpImage, 0, _Size);
break;
case CV_16U:
_Size *= sizeof(uint16_t);
tpImage->vpImage = (uint16_t *)malloc(_Size);
if (NULL == tpImage->vpImage)
return FUNC_NOT_ENOUGH_MEM;
memset(tpImage->vpImage, 0, _Size);
break;
case CV_16S:
_Size *= sizeof(int16_t);
tpImage->vpImage = (int16_t *)malloc(_Size);
if (NULL == tpImage->vpImage)
return FUNC_NOT_ENOUGH_MEM;
memset(tpImage->vpImage, 0, _Size);
break;
case CV_32S:
_Size *= sizeof(int32_t);
tpImage->vpImage = (int32_t *)malloc(_Size);
if (NULL == tpImage->vpImage)
return FUNC_NOT_ENOUGH_MEM;
memset(tpImage->vpImage, 0, _Size);
break;
case CV_32F:
_Size *= sizeof(float_t);
tpImage->vpImage = (float_t *)malloc(_Size);
if (NULL == tpImage->vpImage)
return FUNC_NOT_ENOUGH_MEM;
memset(tpImage->vpImage, 0, _Size);
break;
case CV_64F:
_Size *= sizeof(double_t);
tpImage->vpImage = (double_t *)malloc(_Size);
if (NULL == tpImage->vpImage)
return FUNC_NOT_ENOUGH_MEM;
memset(tpImage->vpImage, 0, _Size);
break;
default:
//no support format
return FUNC_CANNOT_USE;
}
}
//拷贝数据
memcpy(tpImage->vpImage, src.data, _Size);
memcpy(tpImage->vpImage, _src.data, _Size);
return FUNC_OK;
}
......@@ -183,14 +186,88 @@ int eyemImageReadRaw(const char *filename, int iWidth, int iHeight, int iDepth,
return FUNC_OK;
}
int eyemCvtImageType(EyemImage tpImage, int rType, double alpha, double beta, EyemImage &tpDstImg)
int eyemCvtImageType(EyemImage tpImage, const char *ccSubType, double alpha, double beta, EyemImage &tpDstImg)
{
CV_Assert(NULL != tpImage.vpImage);
cv::Mat _src(tpImage.iHeight, tpImage.iWidth, MAKETYPE(tpImage.iDepth, tpImage.iChannels), tpImage.vpImage);
//如果预先分配了内存,则先释放
if (NULL != tpDstImg.vpImage)
tpDstImg.iWidth = 0; tpDstImg.iHeight = 0; tpDstImg.iDepth = 0; tpDstImg.iChannels = 0; free(tpDstImg.vpImage), tpDstImg.vpImage = NULL;
//内存尺寸(图像数据类型转换不涉及通道、尺寸)
int _Size = tpImage.iWidth*tpImage.iHeight*tpImage.iChannels;
//图像信息
if (NULL == tpDstImg.vpImage) {
tpDstImg.iWidth = tpImage.iWidth; tpDstImg.iHeight = tpImage.iHeight; tpDstImg.iChannels = tpImage.iChannels;
cv::Mat _dst;
//分配所需内存
if (strcmp(ccSubType, "uint8_t") == 0) {
tpDstImg.iDepth = 0;
_Size *= sizeof(uint8_t);
tpDstImg.vpImage = (uint8_t *)malloc(_Size);
if (NULL == tpDstImg.vpImage)
return FUNC_NOT_ENOUGH_MEM;
memset(tpDstImg.vpImage, 0, _Size);
}
else if (strcmp(ccSubType, "int8_t") == 0) {
tpDstImg.iDepth = 1;
_Size *= sizeof(int8_t);
tpDstImg.vpImage = (int8_t *)malloc(_Size);
if (NULL == tpDstImg.vpImage)
return FUNC_NOT_ENOUGH_MEM;
memset(tpDstImg.vpImage, 0, _Size);
}
else if (strcmp(ccSubType, "uint16_t") == 0) {
tpDstImg.iDepth = 2;
_Size *= sizeof(uint16_t);
tpDstImg.vpImage = (uint16_t *)malloc(_Size);
if (NULL == tpDstImg.vpImage)
return FUNC_NOT_ENOUGH_MEM;
memset(tpDstImg.vpImage, 0, _Size);
}
else if (strcmp(ccSubType, "int16_t") == 0) {
tpDstImg.iDepth = 3;
_Size *= sizeof(int16_t);
tpDstImg.vpImage = (int16_t *)malloc(_Size);
if (NULL == tpDstImg.vpImage)
return FUNC_NOT_ENOUGH_MEM;
memset(tpDstImg.vpImage, 0, _Size);
}
else if (strcmp(ccSubType, "int32_t") == 0) {
tpDstImg.iDepth = 4;
_Size *= sizeof(int32_t);
tpDstImg.vpImage = (int32_t *)malloc(_Size);
if (NULL == tpDstImg.vpImage)
return FUNC_NOT_ENOUGH_MEM;
memset(tpDstImg.vpImage, 0, _Size);
}
else if (strcmp(ccSubType, "float_t") == 0) {
tpDstImg.iDepth = 5;
_Size *= sizeof(float_t);
tpDstImg.vpImage = (float_t *)malloc(_Size);
if (NULL == tpDstImg.vpImage)
return FUNC_NOT_ENOUGH_MEM;
memset(tpDstImg.vpImage, 0, _Size);
}
else if (strcmp(ccSubType, "double_t") == 0) {
tpDstImg.iDepth = 6;
_Size *= sizeof(double_t);
tpDstImg.vpImage = (double_t *)malloc(_Size);
if (NULL == tpDstImg.vpImage)
return FUNC_NOT_ENOUGH_MEM;
memset(tpDstImg.vpImage, 0, _Size);
}
else {//no support format
return FUNC_CANNOT_USE;
}
_src.convertTo(_dst, tpDstImg.iDepth, alpha, beta);
//拷贝数据
memcpy(tpDstImg.vpImage, _dst.data, _Size);
return FUNC_OK;
}
......@@ -290,7 +367,7 @@ int eyemImageAdd(EyemImage tpImage1, EyemImage tpImage2, EyemImage &tpDstImg)
cv::add(cv::Mat(tpImage1.iHeight, tpImage1.iWidth, MAKETYPE(tpImage1.iDepth, tpImage1.iChannels), tpImage1.vpImage), \
cv::Mat(tpImage2.iHeight, tpImage2.iWidth, MAKETYPE(tpImage2.iDepth, tpImage2.iChannels), tpImage2.vpImage), _dst);
//如果预先分配了内存,则判断基本信息是否符合
//如果预先分配了内存,则判断属性是否符合
if (NULL != tpDstImg.vpImage)
CV_Assert(_dst.cols == tpDstImg.iWidth && _dst.rows == tpDstImg.iHeight && _dst.depth() == tpDstImg.iDepth && _dst.channels() == tpDstImg.iChannels);
......@@ -447,6 +524,8 @@ int eyemImageSub(EyemImage tpImageMinuend, EyemImage tpImageSubtrahend, EyemImag
//拷贝数据
memcpy(tpDstImg.vpImage, _dst.data, _Size);
cv::Mat diff(tpDstImg.iHeight, tpDstImg.iWidth, MAKETYPE(tpDstImg.iDepth, tpDstImg.iChannels), tpDstImg.vpImage);
return FUNC_OK;
}
......@@ -620,4 +699,5 @@ void eyemImageFree(void *vpImage)
{
//must be free 空指针也没关系
free(vpImage);
vpImage = NULL;
}
\ No newline at end of file
......@@ -783,8 +783,9 @@ extern "C" {
EXPORTS int eyemImageRead(const char *filename, int iFalgs, EyemImage *ucpImage);
EXPORTS int eyemImageReadRaw(const char *filename, int iWidth, int iHeight, int iDepth, EyemImage *tpImage);
EXPORTS int eyemImageMalloc(int iWidth, int iHeight, int iChannels, const char *ccSubType, EyemImage *tpImage);
EXPORTS int eyemImageCopy(EyemImage &tpSrcImg, EyemImage &tpDstImg);
EXPORTS int eyemCvtImageColor(EyemImage tpImage, int iCCodes, EyemImage &tpDstImg);
EXPORTS int eyemCvtImageType(EyemImage tpImage, int rType, double alpha, double beta, EyemImage &tpDstImg);
EXPORTS int eyemCvtImageType(EyemImage tpImage, const char *ccSubType, double alpha, double beta, EyemImage &tpDstImg);
EXPORTS int eyemImageAdd(EyemImage tpImage1, EyemImage tpImage2, EyemImage &tpDstImg);
EXPORTS int eyemImageSub(EyemImage tpImageMinuend, EyemImage tpImageSubtrahend, EyemImage &tpDstImg);
EXPORTS int eyemImageDiv(EyemImage tpImage1, EyemImage tpImage2, EyemImage &tpDstImg);
......
支持 Markdown 格式
你添加了 0 到此讨论。请谨慎行事。
Finish editing this message first!