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

📄 chenimagematlab.cpp

📁 LDPC码的实现,包括编码器和解码器,使用了DCT.
💻 CPP
字号:
// ChenImageMatlab.cpp

#include <ChenImage.h>
#include <ChenImageMatlab.h>

Engine* pEngine = NULL;

int ChenImageMatlab_openEngine()
{
	pEngine = engOpen("\0");
	return GOOD_RETURN;
}

int ChenImageMatlab_closeEngine()
{
	if (pEngine != NULL) {
		engClose(pEngine);
		pEngine = NULL;
	}
	return GOOD_RETURN;
}

Engine* ChenImageMatlab_getEngine()
{
	if (pEngine == NULL) ChenImageMatlab_openEngine();
	return pEngine;
}

int ChenImageMatlab_putImageInWorkspace(const int nWidth, const int nHeight, const int nChannels, const short* pImage, const char* pVarName)
{
	bool bSuccess = true;

	Engine* pEngine = ChenImageMatlab_getEngine();
	engEvalString(pEngine, "clear all");

	mxArray* pMxImgPlace = mxCreateDoubleMatrix(1, nWidth*nHeight*nChannels, mxREAL);
	double* pDst = mxGetPr(pMxImgPlace); // (row,col,ch) = ch*(rows*height) + col*rows + row
	for (int nCol = 0; nCol < nWidth; nCol++) {
		for (int nRow = 0; nRow < nHeight; nRow++) {
			for (int nChannel = 0; nChannel < nChannels; nChannel++) {
				int nPixSrc = (nRow*nWidth + nCol)*nChannels + nChannel;
				int nPixDst = nCol*nHeight + nRow + nChannel*(nHeight*nWidth);
				pDst[nPixDst] = pImage[nPixSrc];
			}
		}
	}
	char pTextBuffer[1024];
	engPutVariable(pEngine, pVarName, pMxImgPlace);
	sprintf(pTextBuffer, "imgWidth=%d;", nWidth);
	engEvalString(pEngine, pTextBuffer);
	sprintf(pTextBuffer, "imgHeight=%d;", nHeight);
	engEvalString(pEngine, pTextBuffer);
	sprintf(pTextBuffer, "imgChannels=%d;", nChannels);
	engEvalString(pEngine, pTextBuffer);
	sprintf(pTextBuffer, "%s = reshape(%s,imgHeight,imgWidth,imgChannels);", pVarName, pVarName);
	engEvalString(pEngine, pTextBuffer);

	if (pMxImgPlace != NULL)
	{
		mxDestroyArray(pMxImgPlace); // Haiguang
		pMxImgPlace = NULL;
	}

	return bSuccess ? GOOD_RETURN : BAD_RETURN;
}

int ChenImageMatlab_putImageInWorkspace(const int nWidth, const int nHeight, const int nChannels, const float* pImage, const char* pVarName)
{
	bool bSuccess = true;

	Engine* pEngine = ChenImageMatlab_getEngine();
	engEvalString(pEngine, "clear all");
	mxArray* pMxImgPlace = mxCreateDoubleMatrix(1, nWidth*nHeight*nChannels, mxREAL);
	double* pDst = mxGetPr(pMxImgPlace); // (row,col,ch) = ch*(rows*height) + col*rows + row
	for (int nCol = 0; nCol < nWidth; nCol++) {
		for (int nRow = 0; nRow < nHeight; nRow++) {
			for (int nChannel = 0; nChannel < nChannels; nChannel++) {
				int nPixSrc = (nRow*nWidth + nCol)*nChannels + nChannel;
				int nPixDst = nCol*nHeight + nRow + nChannel*(nHeight*nWidth);
				pDst[nPixDst] = pImage[nPixSrc];
			}
		}
	}
	char pTextBuffer[1024];
	engPutVariable(pEngine, pVarName, pMxImgPlace);
	sprintf(pTextBuffer, "imgWidth=%d;", nWidth);
	engEvalString(pEngine, pTextBuffer);
	sprintf(pTextBuffer, "imgHeight=%d;", nHeight);
	engEvalString(pEngine, pTextBuffer);
	sprintf(pTextBuffer, "imgChannels=%d;", nChannels);
	engEvalString(pEngine, pTextBuffer);
	sprintf(pTextBuffer, "%s = reshape(%s,imgHeight,imgWidth,imgChannels);", pVarName, pVarName);
	engEvalString(pEngine, pTextBuffer);

	if (pMxImgPlace != NULL)
	{
		mxDestroyArray(pMxImgPlace); // Haiguang
		pMxImgPlace = NULL;
	}

	return bSuccess ? GOOD_RETURN : BAD_RETURN;
}


int ChenImageMatlab_getImageFromWorkspace(const int nWidth, const int nHeight, const int nChannels, const char* pVarName, short* pImage)
{
	bool bSuccess = true;
	char pTextBuffer[1024];

	int nPixelsPerChannel = nWidth * nHeight;
	int nPixels = nPixelsPerChannel * nChannels;
	Engine* pEngine = ChenImageMatlab_getEngine();
	sprintf(pTextBuffer, "%sDouble = double(%s);", pVarName, pVarName);
	engEvalString(pEngine, pTextBuffer);
	sprintf(pTextBuffer, "%sDouble", pVarName);
	mxArray* pMxImgLoad = engGetVariable(pEngine, pTextBuffer);
	if (pMxImgLoad == NULL) return BAD_RETURN;

	double* pSrc = mxGetPr(pMxImgLoad); // (row,col,ch) = ch*(rows*height) + col*rows + row
	short* pDst = pImage; // (row,col,ch) = (row*width + col)*channels * ch
	for (int nCol = 0; nCol < nWidth; nCol++) {
		for (int nRow = 0; nRow < nHeight; nRow++) {
			for (int nChannel = 0; nChannel < nChannels; nChannel++) {
				int nPixSrc = nCol*nHeight + nRow + nChannel*nPixelsPerChannel;
				int nPixDst = (nRow*nWidth + nCol)*nChannels + nChannel;
				pDst[nPixDst] = ROUND(pSrc[nPixSrc]);
			}
		}
	}

	if (pMxImgLoad != NULL)
	{
		mxDestroyArray(pMxImgLoad); // Haiguang
		pMxImgLoad = NULL;
	}

	return bSuccess ? GOOD_RETURN : BAD_RETURN;
}


int ChenImageMatlab_getImageFromWorkspace(const int nWidth, const int nHeight, const int nChannels, const char* pVarName, float* pImage)
{
	bool bSuccess = true;
	char pTextBuffer[1024];

	int nPixelsPerChannel = nWidth * nHeight;
	int nPixels = nPixelsPerChannel * nChannels;
	Engine* pEngine = ChenImageMatlab_getEngine();
	sprintf(pTextBuffer, "%sDouble = double(%s);", pVarName, pVarName);
	engEvalString(pEngine, pTextBuffer);
	sprintf(pTextBuffer, "%sDouble", pVarName);
	mxArray* pMxImgLoad = engGetVariable(pEngine, pTextBuffer);
	if (pMxImgLoad == NULL) return BAD_RETURN;

	double* pSrc = mxGetPr(pMxImgLoad); // (row,col,ch) = ch*(rows*height) + col*rows + row
	float*  pDst = pImage; // (row,col,ch) = (row*width + col)*channels * ch
	for (int nCol = 0; nCol < nWidth; nCol++) {
		for (int nRow = 0; nRow < nHeight; nRow++) {
			for (int nChannel = 0; nChannel < nChannels; nChannel++) {
				int nPixSrc = nCol*nHeight + nRow + nChannel*nPixelsPerChannel;
				int nPixDst = (nRow*nWidth + nCol)*nChannels + nChannel;
				pDst[nPixDst] = (float)(pSrc[nPixSrc]);
			}
		}
	}

	if (pMxImgLoad != NULL)
	{
		mxDestroyArray(pMxImgLoad); // Haiguang
		pMxImgLoad = NULL;
	}

	return bSuccess ? GOOD_RETURN : BAD_RETURN;
}

int ChenImageMatlab_loadImageFromFile(const char* pImgFileName, int& nWidth, int& nHeight, int& nChannels, short*& pImg)
{
	bool bSuccess = true;
	char pTextBuffer[1024];

	// Try to load image using Matlab
	Engine* pEngine = ChenImageMatlab_getEngine();
	engEvalString(pEngine, "clear imgLoad;");
	sprintf(pTextBuffer, "imgLoad = imread('%s');", pImgFileName);
	engEvalString(pEngine, pTextBuffer);
	engEvalString(pEngine, "imgWidth = size(imgLoad,2);");
	engEvalString(pEngine, "imgHeight = size(imgLoad,1);");
	engEvalString(pEngine, "imgChannels = size(imgLoad,3);");
	mxArray* pMxImgWidth = engGetVariable(pEngine, "imgWidth");
	mxArray* pMxImgHeight = engGetVariable(pEngine, "imgHeight");
	mxArray* pMxImgChannels = engGetVariable(pEngine, "imgChannels");
	if (pMxImgWidth == NULL || pMxImgHeight == NULL || pMxImgChannels == NULL) return BAD_RETURN;

	// Output image and parameters
	nWidth = ROUND(*mxGetPr(pMxImgWidth));
	nHeight = ROUND(*mxGetPr(pMxImgHeight));
	nChannels = ROUND(*mxGetPr(pMxImgChannels));
	if (nHeight <= 0 || nWidth <= 0 || nChannels <= 0) return BAD_RETURN;
	int nPixelsPerChannel = nWidth * nHeight;
	int nPixels = nPixelsPerChannel * nChannels;
	pImg = new short[nPixels];
	RECORD_SUCCESS( ChenImageMatlab_getImageFromWorkspace(nWidth, nHeight, nChannels, "imgLoad", pImg), bSuccess );

	mxDestroyArray(pMxImgWidth); //Haiguang
	mxDestroyArray(pMxImgHeight);
	mxDestroyArray(pMxImgChannels);


	return bSuccess ? GOOD_RETURN : BAD_RETURN;
}

int ChenImageMatlab_saveImageToFile(const char* pImgFileName, const int nWidth, const int nHeight, const int nChannels, const short* pImg)
{
	bool bSuccess = true;

	// Try to save image in Matlab
	char pTextBuffer[1024];
	Engine* pEngine = ChenImageMatlab_getEngine();
	RECORD_SUCCESS( ChenImageMatlab_putImageInWorkspace(nWidth, nHeight, nChannels, pImg, "imgSave"), bSuccess );
	sprintf(pTextBuffer, "imgSaveLocation = '%s';", pImgFileName);
	engEvalString(pEngine, pTextBuffer);
	sprintf(pTextBuffer, "imwrite(uint8(imgSave), imgSaveLocation);");
	engEvalString(pEngine, pTextBuffer);

	return bSuccess ? GOOD_RETURN : BAD_RETURN;
}

int ChenImageMatlab_saveImageJPEG(const char* pImgFileName, const int nWidth, const int nHeight, const int nChannels, const short* pImg,  const int nQuality, const bool bLossless, const int nBitDepth)
{
	bool bSuccess = true;

	// Try to save image in JPEG using Matlab
	char pTextBuffer[1024];
	Engine* pEngine = ChenImageMatlab_getEngine();
	RECORD_SUCCESS( ChenImageMatlab_putImageInWorkspace(nWidth, nHeight, nChannels, pImg, "imgSave"), bSuccess );
	sprintf(pTextBuffer, "imgSaveLocation = '%s';", pImgFileName);
	engEvalString(pEngine, pTextBuffer);
	sprintf(pTextBuffer, "imwrite(uint8(imgSave), imgSaveLocation, 'JPEG', 'Quality', %d, 'Mode', %s, 'BitDepth', %d);", nQuality, (bLossless ? "'lossless'" : "'lossy'"), nBitDepth);
	engEvalString(pEngine, pTextBuffer);

	return bSuccess ? GOOD_RETURN : BAD_RETURN;
}


//int ChenImageMatlab_saveImageJPEG2K(const char* pImgFileName, const int nWidth, const int nHeight, const int nChannels, const short* pImg, const int nLayers, const float* pLayerRates)
//{
//	bool bSuccess = true;
//
//	// Try to save temporary image using Matlab
//	char pTextBuffer[1024], pTextBuffer2[1024];
//	sprintf(pTextBuffer, "%s\\temp.ppm", KAKADU_BIN);
//	RECORD_SUCCESS( ChenImageMatlab_saveImageToFile(pTextBuffer, nWidth, nHeight, nChannels, pImg), bSuccess );
//
//	// Try to encode using Kakadu codec
//	sprintf(pTextBuffer, "%s\\kdu_compress.exe -i \"%s\\temp.ppm\" -o \"%s\" -quiet -rate ", KAKADU_BIN, KAKADU_BIN, pImgFileName);
//	for (int nLayer = 0; nLayer < nLayers; nLayer++) {
//		sprintf(pTextBuffer2, "%f%s", pLayerRates[nLayer], (nLayer == nLayers-1 ? "" : ","));
//		strcat(pTextBuffer, pTextBuffer2);
//	}
//	system(pTextBuffer);
//	sprintf(pTextBuffer, "del %s\\temp.ppm", KAKADU_BIN);
//	system(pTextBuffer);
//
//	return bSuccess ? GOOD_RETURN : BAD_RETURN;
//}
//
//int ChenImageMatlab_loadImageJPEG2K(const char* pStreamName, int& nWidth, int& nHeight, int& nChannels, short*& pImg)
//{
//	bool bSuccess = true;
//
//	// Try to decode using Kakadu codec
//	char pTextBuffer[1024];
//	sprintf(pTextBuffer, "%s\\kdu_expand.exe -i \"%s\" -o \"%s\\temp.ppm\" -quiet", KAKADU_BIN, pStreamName, KAKADU_BIN);
//	system(pTextBuffer);
//
//	// Try to load temporary image using Matlab
//	sprintf(pTextBuffer, "%s\\temp.ppm", KAKADU_BIN);
//	RECORD_SUCCESS( ChenImageMatlab_loadImageFromFile(pTextBuffer, nWidth, nHeight, nChannels, pImg), bSuccess );
//
//	return bSuccess ? GOOD_RETURN : BAD_RETURN;
//}




int ChenImageMatlab_showImage(const int nWidth, const int nHeight, const int nChannels, short* pImage, const char* pName, bool bNewFigure)
{
	bool bSuccess = true;

	RECORD_SUCCESS( ChenImageMatlab_putImageInWorkspace(nWidth, nHeight, nChannels, pImage, "imgPlace"), bSuccess );

	char pTextBuffer[1024];
	if (bNewFigure) {
		engEvalString(pEngine, "figure;");
	}
	engEvalString(pEngine, "imshow(uint8(imgPlace));");
	if (pName != NULL) {
		sprintf(pTextBuffer, "title('%s');", pName);
		engEvalString(pEngine, pTextBuffer);
	}

	return bSuccess ? GOOD_RETURN : BAD_RETURN;
}

int ChenImageMatlab_imageFileSize(const char* pImgFileName, int* pBits)
{
	bool bSuccess = true;

	char pTextBuffer[1024];
	Engine* pEngine = ChenImageMatlab_getEngine();
	sprintf(pTextBuffer, "info = dir('%s');", pImgFileName);
	engEvalString(pEngine, pTextBuffer);
	engEvalString(pEngine, "infoSize = info.bytes * 8;");
	mxArray* pMxInfoSize = engGetVariable(pEngine, "infoSize");
	*pBits = ROUND(*mxGetPr(pMxInfoSize));

	mxDestroyArray(pMxInfoSize); // Haiguang

	return bSuccess ? GOOD_RETURN : BAD_RETURN;
}

int ChenImageMatlab_systemCommand(const char* pCommand)
{
	Engine* pEngine = ChenImageMatlab_getEngine();
	char pTextBuffer[1024];
	sprintf(pTextBuffer, "[status, result] = system('%s');", pCommand);
	engEvalString(pEngine, pTextBuffer);

	return GOOD_RETURN;
}

⌨️ 快捷键说明

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