/*public DoubleImage KIMembed(DoubleImage image, Watermark initialWatermark) * { * DoubleImage copy = image.Copy(); * int maxWatermarkLength = GetOptimalWatermarkLength(copy); * if (maxWatermarkLength < initialWatermark.Length) * throw new Exception("Watermark length is too big to embed it in the image."); * * DoubleImage result = KIMembedComponent(image, initialWatermark, DoubleImage.ColorComponent.Blue); * if (p < initialWatermark.Length) * result = KIMembedComponent(result, initialWatermark, DoubleImage.ColorComponent.Red); * if (p < initialWatermark.Length) * result = KIMembedComponent(result, initialWatermark, DoubleImage.ColorComponent.Green); * p = 0; * return result; * }*/ private DoubleImage KIMembedComponent(DoubleImage image, Watermark initialWatermark, DoubleImage.ColorComponent colorComponent) { // Get blue component from image double[,] blueComponent = image.GetColorComponent(colorComponent); // Transfrom blue component with wavelet transformation double[,] waveletTransformedComponent = Wavelet.Transform(blueComponent, 3); // Get coefficients of transformation double[,] approx3 = Wavelet.GetCoefficient(waveletTransformedComponent, Wavelet.Coefficients.Approximation, 3); double[,] detailHor3 = Wavelet.GetCoefficient(waveletTransformedComponent, Wavelet.Coefficients.Horizontal, 3); double[,] detailVert3 = Wavelet.GetCoefficient(waveletTransformedComponent, Wavelet.Coefficients.Vertical, 3); double[,] detailDiag3 = Wavelet.GetCoefficient(waveletTransformedComponent, Wavelet.Coefficients.Diagonal, 3); double[,] detailHor2 = Wavelet.GetCoefficient(waveletTransformedComponent, Wavelet.Coefficients.Horizontal, 2); double[,] detailVert2 = Wavelet.GetCoefficient(waveletTransformedComponent, Wavelet.Coefficients.Vertical, 2); double[,] detailDiag2 = Wavelet.GetCoefficient(waveletTransformedComponent, Wavelet.Coefficients.Diagonal, 2); double[,] detailHor1 = Wavelet.GetCoefficient(waveletTransformedComponent, Wavelet.Coefficients.Horizontal, 1); double[,] detailVert1 = Wavelet.GetCoefficient(waveletTransformedComponent, Wavelet.Coefficients.Vertical, 1); double[,] detailDiag1 = Wavelet.GetCoefficient(waveletTransformedComponent, Wavelet.Coefficients.Diagonal, 1); // Get thresholds double threshold1 = Threshold.GetThreshold(detailHor1, detailVert1, detailDiag1); double threshold2 = Threshold.GetThreshold(detailHor2, detailVert2, detailDiag2); double threshold3 = Threshold.GetThreshold(detailHor3, detailVert3, detailDiag3, approx3); Watermark watermark = TransformWatermark(initialWatermark); // Embed watermark into coefficients //int p = 0; p = EmbedInCoef(coefApprox, ref approx3, threshold3, watermark, p); p = EmbedInCoef(coef3Level, ref detailHor3, threshold3, watermark, p); p = EmbedInCoef(coef3Level, ref detailVert3, threshold3, watermark, p); p = EmbedInCoef(coef3Level, ref detailDiag3, threshold3, watermark, p); p = EmbedInCoef(coef2Level, ref detailHor2, threshold2, watermark, p); p = EmbedInCoef(coef2Level, ref detailVert2, threshold2, watermark, p); p = EmbedInCoef(coef2Level, ref detailDiag2, threshold2, watermark, p); p = EmbedInCoef(coef1Level, ref detailHor1, threshold1, watermark, p); p = EmbedInCoef(coef1Level, ref detailVert1, threshold1, watermark, p); p = EmbedInCoef(coef1Level, ref detailDiag1, threshold1, watermark, p); Console.WriteLine("watermark length {0}", watermark.Length); Console.WriteLine("p {0}", p); // Restore blue component from coefficients double[,] componentWithWatermark = new double[blueComponent.GetLength(0), blueComponent.GetLength(1)]; componentWithWatermark = Wavelet.SetCoefficient(componentWithWatermark, Wavelet.Coefficients.Approximation, 3, approx3); componentWithWatermark = Wavelet.SetCoefficient(componentWithWatermark, Wavelet.Coefficients.Horizontal, 3, detailHor3); componentWithWatermark = Wavelet.SetCoefficient(componentWithWatermark, Wavelet.Coefficients.Vertical, 3, detailVert3); componentWithWatermark = Wavelet.SetCoefficient(componentWithWatermark, Wavelet.Coefficients.Diagonal, 3, detailDiag3); componentWithWatermark = Wavelet.SetCoefficient(componentWithWatermark, Wavelet.Coefficients.Horizontal, 2, detailHor2); componentWithWatermark = Wavelet.SetCoefficient(componentWithWatermark, Wavelet.Coefficients.Vertical, 2, detailVert2); componentWithWatermark = Wavelet.SetCoefficient(componentWithWatermark, Wavelet.Coefficients.Diagonal, 2, detailDiag2); componentWithWatermark = Wavelet.SetCoefficient(componentWithWatermark, Wavelet.Coefficients.Horizontal, 1, detailHor1); componentWithWatermark = Wavelet.SetCoefficient(componentWithWatermark, Wavelet.Coefficients.Vertical, 1, detailVert1); componentWithWatermark = Wavelet.SetCoefficient(componentWithWatermark, Wavelet.Coefficients.Diagonal, 1, detailDiag1); double[,] untrasformedComponent = Wavelet.Untransfrom(componentWithWatermark, 3); // Embed blue component into initial image image = image.UpdateColorComponent(colorComponent, untrasformedComponent); return(image); }
static void Main(string[] args) { var keyWord = "roses"; var inPicture = keyWord + ".jpg"; var changedPicture = keyWord + "_changed.jpg"; var qr = "QRcode_phone.png"; var qr_extracted = keyWord + "_QR.jpg"; DoubleImage image; using (Bitmap imageBM = new Bitmap(inPicture)) //lena_changed.jpg { image = new DoubleImage(imageBM); } /*Watermark watermark; * using (Bitmap imageBM = new Bitmap(qr)) //lena_changed.jpg * { * DoubleImage imageTemp = new DoubleImage(imageBM); * watermark = new Watermark(imageTemp); * } * * JRKimAlgorithm algorithm = new JRKimAlgorithm(); * DoubleImage changed = algorithm.KIMembed(image, watermark); * Bitmap result = changed.ToBitmap(1, 0); * result.Save(changedPicture); */ DoubleImage black; using (Bitmap imageBM = new Bitmap("black_cr.jpg")) //lena_changed.jpg { black = new DoubleImage(imageBM); } double[,] blue = image.GetColorComponent(DoubleImage.ColorComponent.Blue); double[,] red = image.GetColorComponent(DoubleImage.ColorComponent.Red); double[,] green = image.GetColorComponent(DoubleImage.ColorComponent.Green); /*double[,] changed_blue = Wavelet.Transform(blue, 1); * double[,] changed_green = Wavelet.Transform(green, 1); * double[,] changed_red = Wavelet.Transform(red, 1);*/ //black.UpdateColorComponent(DoubleImage.ColorComponent.Red, red); black.UpdateColorComponent(DoubleImage.ColorComponent.Red, red); // black.UpdateColorComponent(DoubleImage.ColorComponent.Green, green); Bitmap result = black.ToBitmap(1, 0); result.Save("roses_red.jpg"); /*DoubleImage initialImage, changedImage; * * using (Bitmap imageBitmap = new Bitmap(inPicture)) //lena_changed.jpg * { * initialImage = new DoubleImage(imageBitmap); * } * * using (Bitmap imageBitmap = new Bitmap(changedPicture)) * { * changedImage = new DoubleImage(imageBitmap); * } * * JRKimAlgorithm algorithm = new JRKimAlgorithm(); * DoubleImage watermark = algorithm.KIMextract(initialImage, changedImage, 100, 100); * Bitmap result = watermark.ToBitmap(1,0); * result.Save(qr_extracted);*/ Console.WriteLine("Done"); Console.ReadKey(); }
private Watermark KIMextractComponent(DoubleImage initialImage, DoubleImage changedImage, int widthWatermark, int heigthWatermark, DoubleImage.ColorComponent colorComponent) { // Get blue component from changed image double[,] blueComponentChanged = changedImage.GetColorComponent(colorComponent); // Transfrom blue component of changed image with wavelet transformation double[,] waveletTransformedChangedComponent = Wavelet.Transform(blueComponentChanged, 3); // Get coefficients of transformation double[,] approx3 = Wavelet.GetCoefficient(waveletTransformedChangedComponent, Wavelet.Coefficients.Approximation, 3); double[,] detailHor3 = Wavelet.GetCoefficient(waveletTransformedChangedComponent, Wavelet.Coefficients.Horizontal, 3); double[,] detailVert3 = Wavelet.GetCoefficient(waveletTransformedChangedComponent, Wavelet.Coefficients.Vertical, 3); double[,] detailDiag3 = Wavelet.GetCoefficient(waveletTransformedChangedComponent, Wavelet.Coefficients.Diagonal, 3); double[,] detailHor2 = Wavelet.GetCoefficient(waveletTransformedChangedComponent, Wavelet.Coefficients.Horizontal, 2); double[,] detailVert2 = Wavelet.GetCoefficient(waveletTransformedChangedComponent, Wavelet.Coefficients.Vertical, 2); double[,] detailDiag2 = Wavelet.GetCoefficient(waveletTransformedChangedComponent, Wavelet.Coefficients.Diagonal, 2); double[,] detailHor1 = Wavelet.GetCoefficient(waveletTransformedChangedComponent, Wavelet.Coefficients.Horizontal, 1); double[,] detailVert1 = Wavelet.GetCoefficient(waveletTransformedChangedComponent, Wavelet.Coefficients.Vertical, 1); double[,] detailDiag1 = Wavelet.GetCoefficient(waveletTransformedChangedComponent, Wavelet.Coefficients.Diagonal, 1); // Get blue component from initial image double[,] blueComponentInitial = initialImage.GetColorComponent(colorComponent); // Transfrom blue component of initial image with wavelet transformation double[,] waveletTransformedComponent = Wavelet.Transform(blueComponentInitial, 3); // Get coefficients of transformation double[,] approx3Initial = Wavelet.GetCoefficient(waveletTransformedComponent, Wavelet.Coefficients.Approximation, 3); double[,] detailHor3Initial = Wavelet.GetCoefficient(waveletTransformedComponent, Wavelet.Coefficients.Horizontal, 3); double[,] detailVert3Initial = Wavelet.GetCoefficient(waveletTransformedComponent, Wavelet.Coefficients.Vertical, 3); double[,] detailDiag3Initial = Wavelet.GetCoefficient(waveletTransformedComponent, Wavelet.Coefficients.Diagonal, 3); double[,] detailHor2Initial = Wavelet.GetCoefficient(waveletTransformedComponent, Wavelet.Coefficients.Horizontal, 2); double[,] detailVert2Initial = Wavelet.GetCoefficient(waveletTransformedComponent, Wavelet.Coefficients.Vertical, 2); double[,] detailDiag2Initial = Wavelet.GetCoefficient(waveletTransformedComponent, Wavelet.Coefficients.Diagonal, 2); double[,] detailHor1Initial = Wavelet.GetCoefficient(waveletTransformedComponent, Wavelet.Coefficients.Horizontal, 1); double[,] detailVert1Initial = Wavelet.GetCoefficient(waveletTransformedComponent, Wavelet.Coefficients.Vertical, 1); double[,] detailDiag1Initial = Wavelet.GetCoefficient(waveletTransformedComponent, Wavelet.Coefficients.Diagonal, 1); // Get thresholds from initial image double threshold1 = Threshold.GetThreshold(detailHor1Initial, detailVert1Initial, detailDiag1Initial); double threshold2 = Threshold.GetThreshold(detailHor2Initial, detailVert2Initial, detailDiag2Initial); double threshold3 = Threshold.GetThreshold(detailHor3Initial, detailVert3Initial, detailDiag3Initial, approx3Initial); int watermarkSize = widthWatermark * heigthWatermark; Watermark watermark = new Watermark(watermarkSize); // Extract watermark from coefficients //int p = 0; p = ExtractFromCoef(coefApprox, approx3Initial, approx3, threshold3, p, watermarkSize, ref watermark); p = ExtractFromCoef(coef3Level, detailHor3Initial, detailHor3, threshold3, p, watermarkSize, ref watermark); p = ExtractFromCoef(coef3Level, detailVert3Initial, detailVert3, threshold3, p, watermarkSize, ref watermark); p = ExtractFromCoef(coef3Level, detailDiag3Initial, detailDiag3, threshold3, p, watermarkSize, ref watermark); p = ExtractFromCoef(coef2Level, detailHor2Initial, detailHor2, threshold2, p, watermarkSize, ref watermark); p = ExtractFromCoef(coef2Level, detailVert2Initial, detailVert2, threshold2, p, watermarkSize, ref watermark); p = ExtractFromCoef(coef2Level, detailDiag2Initial, detailDiag2, threshold2, p, watermarkSize, ref watermark); p = ExtractFromCoef(coef1Level, detailHor1Initial, detailHor1, threshold1, p, watermarkSize, ref watermark); p = ExtractFromCoef(coef1Level, detailVert1Initial, detailVert1, threshold1, p, watermarkSize, ref watermark); p = ExtractFromCoef(coef1Level, detailDiag1Initial, detailDiag1, threshold1, p, watermarkSize, ref watermark); watermark = UnTransformWatermark(watermark); return(watermark); //DoubleImage watermarkImage = watermark.ToDoubleImage(widthWatermark, heigthWatermark); //return watermarkImage; }
public static DoubleImage FillWithSmallerImage(DoubleImage bigImage, DoubleImage smallImage) { if (smallImage.Height > bigImage.Height || smallImage.Width > bigImage.Width) { return(smallImage); } double[,] greenSmall = smallImage.GetColorComponent(ColorComponent.Red); double[,] greenBig = bigImage.GetColorComponent(ColorComponent.Red); int jEnd = greenSmall.GetLength(1) - 1; int iEnd = greenSmall.GetLength(0) - 1; int countOfChecking = Math.Min(smallImage.Height, smallImage.Width) / 2; int iStart = 0; int jStart = 0; bool isFound = false; for (int i = bigImage.Height - 1; i >= 0; i--) { if (isFound) { break; } for (int j = bigImage.Width - 1; j >= 0; j--) { if (greenBig[i, j] == greenSmall[iEnd, jEnd]) { isFound = true; for (int k = 1; k < countOfChecking; k++) { if (greenBig[i - k, j - k] != greenSmall[iEnd - k, jEnd - k]) { isFound = false; break; } } if (isFound) { iStart = i - smallImage.Height; jStart = j - smallImage.Width; Console.WriteLine("iStart {0}", iStart); Console.WriteLine("jStart {0}", jStart); Console.WriteLine("iEnd {0}", iEnd); Console.WriteLine("jEnd {0}", jEnd); break; } } } } DoubleImage updateSmallImage = new DoubleImage(bigImage.Width, bigImage.Height); for (int i = 0; i < bigImage.Width; i++) { for (int j = 0; j < bigImage.Height; j++) { if (iStart <= i && i <= iStart + iEnd && jStart <= j && j <= jStart + jEnd) { DoublePixel pixel = smallImage.GetPixel(i - iStart, j - jStart); updateSmallImage.SetPixel(i, j, pixel.Red, pixel.Green, pixel.Blue); } else { updateSmallImage.SetPixel(i, j, 255, 255, 255); } } } return(updateSmallImage); }