Ejemplo n.º 1
0
//===========================================================================
//===========================================================================
int _tmain(int argc, _TCHAR* argv[])
{
    //Verify command-line usage correctness
    if (argc != 4)
    {
        _tprintf(_T("Use: %s <Input_Image_File_Name> <Output_Image_File_Name> <Confidence_Image_File_Name>\n"), argv[0]);
        return -1;
    }

    //Buffer for the new file names
    TCHAR strNewFileName[0x100];

    //Load and verify that input image is a True-Color one
    KImage *pImage = new KImage(argv[1]);
    if (pImage == NULL || !pImage->IsValid() || pImage->GetBPP() != 24)
    {
        _tprintf(_T("File %s does is not a valid True-Color image!"), argv[0]);
        return -2;
    }
	
    //Apply a Gaussian Blur with small radius to remove obvious noise
    pImage->GaussianBlur(0.5);
    _stprintf_s(strNewFileName, sizeof(strNewFileName) / sizeof(TCHAR), _T("%s_blurred.TIF"), argv[1]);
   // pImage->SaveAs(strNewFileName, SAVE_TIFF_LZW);

    //Convert to grayscale
    KImage *pImageGrayscale = pImage->ConvertToGrayscale();
    //Don't forget to delete the original, now useless image
    delete pImage;

    //Verify conversion success...
    if (pImageGrayscale == NULL || !pImageGrayscale->IsValid() || pImageGrayscale->GetBPP() != 8)
    {
        _tprintf(_T("Conversion to grayscale was not successfull!"));
        return -3;
    }
    //... and save grayscale image
    _stprintf_s(strNewFileName, sizeof(strNewFileName) / sizeof(TCHAR), _T("%s"), argv[2]);
   // pImageGrayscale->SaveAs(strNewFileName, SAVE_TIFF_LZW);
    
    //Request direct access to image pixels in raw format
    BYTE **pDataMatrixGrayscale = NULL;
    if (pImageGrayscale->BeginDirectAccess() && (pDataMatrixGrayscale = pImageGrayscale->GetDataMatrix()) != NULL)
    {
        //If direct access is obtained get image attributes and start processing pixels
        int intWidth = pImageGrayscale->GetWidth();
        int intHeight = pImageGrayscale->GetHeight();

		// Calculate histogram
		int histogram[256], ii;
		for(ii=0; ii < 256; ii++ )
			histogram[ii] = 0;
	
		for (int y = intHeight - 1; y >= 0; y--){
			for (int x = intWidth - 1; x >= 0; x--){
				BYTE &PixelAtXY = pDataMatrixGrayscale[y][x];
				int i = 0xFF & PixelAtXY;	//Full-White & Pixel of image
				histogram[i]++;
			}
		}

			float sumB = 0, sum = 0, Max = 0;
			int weightB = 0, weightF = 0, threshold = 0, mB, mF;
			int total = intWidth * intHeight;	//number of pixels in the image

			for (int t = 0; t < 256; t++) 
				sum = sum + t * histogram[t];
			
			for (int t = 0; t < 256; t++){

				weightB = weightB + histogram[t];	// Weight Background
				if (weightB == 0) 
					continue;

				weightF = total - weightB;			// Weight Foreground
				if (weightF == 0) 
					break;

				sumB = sumB + (float) (t * histogram[t]);

				mB = ((int) sumB) / weightB;            // Mean Background
				mF = ((int) (sum - sumB)) / weightF;    // Mean Foreground

   // Calculate Between Class Variance
				float Between = (float)weightB * (float)weightF * (mB - mF) * (mB - mF);

   // Check if new maximum found
				if (Between > Max) {
					Max = Between;
					threshold = t;
				}
			}

        //Create binary image
        KImage *pImageBinary = new KImage(intWidth, intHeight, 1);
		KImage *pImageConf = new KImage(intWidth, intHeight, 8);

        if ( (pImageBinary->BeginDirectAccess()) && (pImageConf->BeginDirectAccess()) ){
            //Apply a threshold at half the grayscale range (0x00 is Full-Black, 0xFF is Full-White, 0x80 is the Middle-Gray)
            for (int y = intHeight - 1; y >= 0; y--){
                for (int x = intWidth - 1; x >= 0; x--){
                    BYTE PixelAtXY = pImageGrayscale->Get8BPPPixel(x, y);
					BYTE diff;
                    if (PixelAtXY < (BYTE) (threshold)){
                        pImageBinary->Put1BPPPixel(x, y, false);	//if closer to black, set to black
						diff = PixelAtXY - 0x00;	//diferenta dintre pixel-grayscale si valoarea binarizata
					}
                    else{
                        pImageBinary->Put1BPPPixel(x, y, true);		//if closer to white, set to white
						diff = 0xFF - PixelAtXY;	//diferenta dintre pixel-grayscale si valoarea binarizata
					}
				
					BYTE PixelAtXYConf = 255 - diff;	//confidenta este = valoarea cea mai sigura - diferenta
				//	printf("%d ", PixelAtXYConf);
					
					pImageConf->Put8BPPPixel(x,y,PixelAtXYConf);
                }
			}

			

            //Close direct access
            pImageBinary->EndDirectAccess();
			pImageConf->EndDirectAccess();
            
            //Save binarized image
            _stprintf_s(strNewFileName, sizeof(strNewFileName) / sizeof(TCHAR), _T("%s"), argv[2]);
            pImageBinary->SaveAs(strNewFileName, SAVE_TIFF_CCITTFAX4);
			
			//Save confidence image
			_stprintf_s(strNewFileName, sizeof(strNewFileName) / sizeof(TCHAR), _T("%s"), argv[3]);
            pImageConf->SaveAs(strNewFileName, SAVE_TIFF_LZW);

            //Don't forget to delete the binary image
            delete pImageBinary;
			delete pImageConf;
        }
        else
        {
            _tprintf(_T("Unable to obtain direct access in binary image!"));
            return -3;
        }


        //Close direct access
        pImageGrayscale->EndDirectAccess();
    }
    else
    {
        _tprintf(_T("Unable to obtain direct access in grayscale image!"));
        return -4;
    }

    //Don't forget to delete the grayscale image
    delete pImageGrayscale;

    //Return with success
    return 0;
}
Ejemplo n.º 2
0
int main(int argc, char * argv[]) {
	if (argc < 2) {
		printf("You should provide at least the name of the input image!\n");
		return -1;
	}
	Choice processingType;
	switch (argc) {
		case 2:
			processingType = FREEIMAGE;
			printf("Applying FREEIMAGE...\n");
			break;
		case 3:
			processingType = PITIE;
			printf("Applying PITIE...\n");
			break;
		case 4:
			processingType = PITIE;
			printf("Applying PITIE...\n");
			break;
		case 5:
			processingType = GOOCH;
			printf("Applying GOOCH...\n");
			break;
		default:
			processingType = NONE;
			printf("Do not know what processing to apply !\n");
			return -1;
	}

	//Buffer for the new file names
	char strNewFileName[0x100];
	//Load and verify that input image is a True-Color one
	KImage *pImage = new KImage(argv[1]);
	if (pImage == NULL || !pImage->IsValid() || pImage->GetBPP() != 24) {
		printf("File %s does is not a valid True-Color image.\n", argv[1]);
		return -2;
	}
	if (processingType == GOOCH) {
		Color2Gray * clr2Gray = new Color2Gray(pImage);
		if (!clr2Gray) {
			printf("Could not allocate class Color2Gray. \n");
			return -1;
		}
		char buffer[1000];
		memset(buffer, 0, 1000);
		char * charpointer = new char[1000];

		convertToChar(argv[2], &charpointer);
		strcat(buffer, charpointer);
		strcat(buffer, " ");

		convertToChar(argv[3], &charpointer);
		strcat(buffer, charpointer);
		strcat(buffer, " ");

		convertToChar(argv[4], &charpointer);
		strcat(buffer, charpointer);

		//convert image with GOOCH algorithm

		KImage *pImageColor2Gray = clr2Gray->localC2G(buffer);
		sprintf(strNewFileName, "%s_GoochGray_%s_%s_%s.png", argv[1],
			argv[2], argv[3], argv[4]);
		pImageColor2Gray->SaveAs(strNewFileName, SAVE_PNG_DEFAULT);

		//convert image with BASIC algorithm
		KImage *pImageBasicGray = clr2Gray->localC2G(buffer,
			Color2Gray::BASIC_GREY);
		sprintf(strNewFileName, "%s_BasicGray.png", argv[1]);
		pImageBasicGray->SaveAs(strNewFileName, SAVE_PNG_DEFAULT);
	} else if (processingType == PITIE) {
		KImage *paletteImage = new KImage(argv[2]);
		if (paletteImage == NULL || !paletteImage->IsValid() ||
			paletteImage->GetBPP() != 24) {
			printf("Cannot perform Color Grading. \n");
			printf("File %s is not a valid True-Color image.",
				argv[2]);
			return -2;
		}
		ColorTransfer * Pitie = NULL;
		if (argc == 4) {
			char * pEnd = NULL;
			long int iterations = strtol(argv[3], &pEnd, 10);
			if (iterations == 0L) {
				printf("Invalid third argument for Pitie Color Grading.\n");
				printf("Provide a valid iterations number or accept the default value 10.\n");
				return -2;
			}
			Pitie = new ColorTransfer(pImage, paletteImage, iterations);
		} else {
			Pitie = new ColorTransfer(pImage, paletteImage);
		}
		assert(Pitie);
		KImage * recoloredImage = Pitie->applyRecoloring();
		assert(recoloredImage);
		removeExtension(argv[1]);
		argv[2] = removeFolder(argv[2]);
		removeExtension(argv[2]);
		sprintf(strNewFileName, "%s%s_Pitie.png", argv[1], argv[2]);
		printf("Result image saved to: %s\n", strNewFileName);
		recoloredImage->SaveAs(strNewFileName, SAVE_PNG_DEFAULT);
	} else if (processingType == FREEIMAGE) {
		//Convert to grayscale and then BlackAndWhite
		KImage *pImageGrayscale = pImage->ConvertToGrayscale();
		//Don't forget to delete the original, now useless image
		delete pImage;

		//Verify conversion success...
		if (pImageGrayscale == NULL || !pImageGrayscale->IsValid() || pImageGrayscale->GetBPP() != 8) {
			printf("Conversion to grayscale was not successful!");
			return -3;
		}
		//... and save grayscale image
		sprintf(strNewFileName, "%s_grayscale.TIF", argv[1]);
		pImageGrayscale->SaveAs(strNewFileName, SAVE_TIFF_LZW);
		//Request direct access to image pixels in raw format
		BYTE **pDataMatrixGrayscale = NULL;
		if (pImageGrayscale->BeginDirectAccess() &&
			(pDataMatrixGrayscale = pImageGrayscale->GetDataMatrix())
			!= NULL) {
			//If direct access is obtained get image attributes and
			//start processing pixels
			int intWidth = pImageGrayscale->GetWidth();
			int intHeight = pImageGrayscale->GetHeight();

			//Create binary image
			KImage *pImageBinary = new KImage(intWidth, intHeight, 1);
			if (pImageBinary->BeginDirectAccess()) {
				//Apply a threshold at half the grayscale range (0x00 is Full-Black, 0xFF is Full-White, 0x80 is the Middle-Gray)
				for (int y = intHeight - 1; y >= 0; y--)
					for (int x = intWidth - 1; x >= 0; x--) {
						//You may use this instead of the line below:
						//    BYTE PixelAtXY = pImageGrayscale->Get8BPPPixel(x, y)
						BYTE &PixelAtXY = pDataMatrixGrayscale[y][x];
						if (PixelAtXY < 0x80)
							//...if closer to black, set to black
							pImageBinary->Put1BPPPixel(x, y, false);
						else
							//...if closer to white, set to white
							pImageBinary->Put1BPPPixel(x, y, true);
					}

				//Close direct access
				pImageBinary->EndDirectAccess();

				//Save binarized image
				sprintf(strNewFileName, "%s_Black_and_White.TIF", argv[1]);
				pImageBinary->SaveAs(strNewFileName, SAVE_TIFF_CCITTFAX4);

				//Don't forget to delete the binary image
				delete pImageBinary;
			} else {
				printf("Unable to obtain direct access in binary image!");
				return -3;
			}

			//Close direct access
			pImageGrayscale->EndDirectAccess();
			delete pImageGrayscale;
		} else {
			printf("Unable to obtain direct access in grayscale image!");
			return -4;
		}
	}

	//Return with success
	return 0;
}
Ejemplo n.º 3
0
int _tmain(int argc, _TCHAR* argv[])
{
    if (argc < 3)
    {
        _tprintf(_T("Wrong usage\n"), argv[0]);
        return -99;
    }

	KImage *outputs[50];
	KImage *confidences[50];
	KImage *original = new KImage(argv[1]);

	if (original == NULL || !original->IsValid()) {
		cout << "no original\n";
		return -1;
	}

	int j = 0;
	for (int i = 2; i < argc; i+= 2) {
		TCHAR strNewFileName[0x100];
         _tprintf(_T("%s %s\n"), argv[i], argv[i + 1]);
		KImage *pImage = new KImage(argv[i]);

		if (pImage != NULL && pImage->IsValid() && pImage->GetWidth() == original->GetWidth() && pImage->GetHeight() == original->GetHeight()) {
			outputs[j] = pImage;
			TCHAR strNewFileName[0x100];
			pImage = new KImage(argv[i + 1]);
			if (pImage != NULL && pImage->IsValid() && pImage->GetWidth() == original->GetWidth() && pImage->GetHeight() == original->GetHeight()) {
				confidences[j] = pImage;
				j++;
			}
		} else {
			std::cout << "Wrong input image " << argv[i] << endl;
		}
	}

	// Confidence
	KImage *pImageBinary = new KImage(confidences[0]->GetWidth(), confidences[0]->GetHeight(), 1);
	if (pImageBinary->BeginDirectAccess()) {
		for(int i = 0; i < j; i++) {
			BYTE **pDataMatrix = NULL;
			if (confidences[i]->BeginDirectAccess() && (pDataMatrix = confidences[i]->GetDataMatrix()) != NULL) {
				for(int x = 0; x < confidences[0]->GetWidth(); x++) {
					for(int y = 0; y < confidences[0]->GetHeight(); y++) {
						BYTE &PixelAtXY = pDataMatrix[y][x];
						if (PixelAtXY < 0x80)
							//...if black, set to black
							pImageBinary->Put1BPPPixel(x, y, true);
						else {
						// //...if not black check the options ;)
							pImageBinary->Put1BPPPixel(x, y, false);
						}
					}
				}
			}
		}

		KImage *pImageAverage = new KImage(confidences[0]->GetWidth(), confidences[0]->GetHeight(), 1);
		if (pImageAverage->BeginDirectAccess()) {
			for(int x = 0; x < outputs[0]->GetWidth(); x++) 
					for(int y = 0; y < outputs[0]->GetHeight(); y++) 
						pImageAverage->Put1BPPPixel(x, y, true);
			
			BYTE **pDataMatrix = NULL;
				
			for(int x = 0; x < outputs[0]->GetWidth(); x++) {
				for(int y = 0; y < outputs[0]->GetHeight(); y++) {
					int count = 0;
					for(int i = 0; i < j; i++) {
						if (outputs[i]->BeginDirectAccess() && (pDataMatrix = outputs[i]->GetDataMatrix()) != NULL) {
							if (outputs[i]->Get1BPPPixel(x, y) == false || pImageBinary->Get1BPPPixel(x, y))
								count++;
						}
					}
					if (count > 0 && count / j >= j / 2)
						pImageAverage->Put1BPPPixel(x, y, false);
				}
			}
		}

		pImageAverage->EndDirectAccess();
		TCHAR strNewFileName[0x100];
		_stprintf_s(strNewFileName, sizeof(strNewFileName) / sizeof(TCHAR), _T("%s_average.TIF"), argv[0]);
        pImageAverage->SaveAs(strNewFileName, SAVE_TIFF_CCITTFAX4);

		// Average
		pImageBinary->EndDirectAccess();
		TCHAR strNewFileNamae[0x100];
		_stprintf_s(strNewFileNamae, sizeof(strNewFileNamae) / sizeof(TCHAR), _T("%s_confidence.TIF"), argv[0]);
        pImageBinary->SaveAs(strNewFileNamae, SAVE_TIFF_CCITTFAX4);

		//Don't forget to delete the binary image
        delete pImageBinary;
    }
    else
    {
        _tprintf(_T("Unable to obtain direct access in binary image!"));
        return -3;
    }
	
	//Return with success
    return 0;
}