简体   繁体   English

使用LibTiff.net裁剪tiff文件时更改图像颜色

[英]Changing in image colors when crop a tiff file using LibTiff.net

I've used the following code to crop some parts of a Tiff image using LibTiff.net library, but the image colors changed to false or pseudo colors.I also tried to change the photometric tag to other possible parameters like YCBCR or Pallete but the results were similar. 我使用以下代码通过LibTiff.net库裁剪了Tiff图像的某些部分,但是图像的颜色变为了假色或伪色。我还尝试将光度标签更改为其他可能的参数,例如YCBCR或Pallete,但是结果相似。 The original image and cropped one are attached below the code. 原始图像和裁剪后的图像附在代码下方。 Any idea would be appreciated to help me solving this problem. 任何想法将不胜感激,以帮助我解决这个问题。

using (input)
        {
            int scanlineSize = input.ScanlineSize();    
            byte[][] buffer = new byte[height][];
            int yy = height/hRatio;
            int xx = width/wRatio;
            int yEnd = yo + yy;

            // read
            int k = 0;
            for (int i = yo; i < yEnd ; i++)
            {
                buffer[k] = new byte[scanlineSize];
                input.ReadScanline(buffer[k], i);
                k++;
            }

            // write
            byte[][] bigHolder = new byte[height][];
            byte[][] holder = new byte[yy][];

            using (Tiff output = Tiff.Open("output.tif", "w"))
            {
                output.SetField(TiffTag.SAMPLESPERPIXEL, samplesPerPixel);
                output.SetField(TiffTag.IMAGEWIDTH, xx);
                output.SetField(TiffTag.IMAGELENGTH, yy);
                output.SetField(TiffTag.BITSPERSAMPLE, bitsPerSample);
                output.SetField(TiffTag.ROWSPERSTRIP, output.DefaultStripSize(0));
                output.SetField(TiffTag.PHOTOMETRIC, photo);
                output.SetField(TiffTag.PLANARCONFIG, PlanarConfig.CONTIG);                

                int j = 0;
                int w = scanlineSize / wRatio;
                for (int i = 0; i < yy; i++)
                {
                    bigHolder[i] = buffer[i].Skip(xo).ToArray();
                    holder[i] = bigHolder[i].Take(w).ToArray();
                    output.WriteScanline(holder[i], j);
                    j++;
                }
            }
        }

原始图像 裁剪图像

I found the problem! 我发现了问题! As the input tiff includes 3 samples per pixel, I skip the number of image pixels instead of samples which happened in line " bigHolder[i] = buffer[i].Skip(xo).ToArray();". 由于输入的tiff每像素包含3个样本,因此我跳过了图像像素的数目,而不是发生在“ bigHolder [i] = buffer [i] .Skip(xo).ToArray();”行中的样本)。 So I've changed the xo to SamplePerPixel * xo and the output tiff remains in true colors. 因此,我已将xo更改为SamplePerPixel * xo,并且输出tiff仍为真彩色。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM