Welcome Guest! You need to login or register to make posts.

Notification

Icon
Error

Options
Go to last post Go to first unread
SebastianR  
#1 Posted : Wednesday, May 10, 2017 5:29:25 AM(UTC)
SebastianR

Rank: Advanced Member

Groups: Member
Joined: 5/10/2017(UTC)
Posts: 33

Thanks: 10 times
Was thanked: 1 time(s) in 1 post(s)
I want to crop very large images according to their colors in each corner. I know of the Transforms.AutoCrop which just needs a color as argument. The problem is I only know the color at runtime. Both approaches from Accessing Pixel Data need to load the bitmap, which is not possible due to the size of the image.
I came up with:
  • Cropping the image to the pixel I want the color of with a pipeline
  • Load the bitmap.
  • Use GetPixel(0,0)
Code:
var memoryStream = new MemoryStream();

using (var imageReader = ImageReader.Create(@"..\Input\largeImage.tif"))
using (var crop = new Crop(0, 0, 1, 1))
using (var writer = new TiffWriter(memoryStream))
{
     Pipeline.Run(imageReader + crop + writer);
}

var resultReader = ImageReader.Create(memoryStream);
var color = resultReader.Frames[0].GetBitmap().GetPixel(0, 0);

But this is an extra pipeline I need to use and it just feels wrong. Furthermore it may not even be possible to start this Pipeline, because the reader is already used in another one which is under construction (the one which uses the autocrop).

Are there any other waysto access a pixels data without loading the entire bitmap?
Fedor  
#2 Posted : Wednesday, May 10, 2017 10:11:10 AM(UTC)
Fedor

Rank: Advanced Member

Groups: Member, Administration, Moderator
Joined: 7/28/2003(UTC)
Posts: 1,572

Thanks: 5 times
Was thanked: 64 time(s) in 63 post(s)
You need to read an image to get its pixels. So you need to open, close, and reopen an image reader. However, you can write a result directly to Bitmap:

Code:
Color color;

using (var imageReader = ImageReader.Create(@"..\..\..\largeImage.tif"))
using (var crop = new Crop(0, 0, 1, 1))
using (var bitmap = new Bitmap())
{
	Pipeline.Run(imageReader + crop + bitmap);

	color = bitmap.GetPixel(0, 0);
}

Console.WriteLine(color);


If you need to get all corner pixels then you can you use a single pipeline:

Code:
Color topLeftColor, topRightColor, bottomLeftColor, bottomRightColor;

using (var imageReader = ImageReader.Create(@"..\..\..\largeImage.tif"))
using (var topLeftCrop = new Crop(0, 0, 1, 1))
using (var topLeftBitmap = new Bitmap())
using (var topRightCrop = new Crop(imageReader.Width - 1, 0, 1, 1))
using (var topRightBitmap = new Bitmap())
using (var bottomLeftCrop = new Crop(0, imageReader.Height - 1, 1, 1))
using (var bottomLeftBitmap = new Bitmap())
using (var bottomRightCrop = new Crop(imageReader.Width - 1, imageReader.Height - 1, 1, 1))
using (var bottomRightBitmap = new Bitmap())
{
	imageReader.Receivers.Add(topLeftCrop);
	topLeftCrop.Receivers.Add(topLeftBitmap);

	imageReader.Receivers.Add(topRightCrop);
	topRightCrop.Receivers.Add(topRightBitmap);

	imageReader.Receivers.Add(bottomLeftCrop);
	bottomLeftCrop.Receivers.Add(bottomLeftBitmap);

	imageReader.Receivers.Add(bottomRightCrop);
	bottomRightCrop.Receivers.Add(bottomRightBitmap);

	imageReader.RunPipeline();
			
	topLeftColor = topLeftBitmap.GetPixel(0, 0);
	topRightColor = topRightBitmap.GetPixel(0, 0);
	bottomLeftColor = bottomLeftBitmap.GetPixel(0, 0);
	bottomRightColor = bottomRightBitmap.GetPixel(0, 0);
}

Console.WriteLine(topLeftColor);
Console.WriteLine(topRightColor);
Console.WriteLine(bottomLeftColor);
Console.WriteLine(bottomRightColor);
Best regards,
Fedor Skvortsov
thanks 1 user thanked Fedor for this useful post.
SebastianR on 5/11/2017(UTC)
SebastianR  
#3 Posted : Thursday, May 11, 2017 11:18:20 PM(UTC)
SebastianR

Rank: Advanced Member

Groups: Member
Joined: 5/10/2017(UTC)
Posts: 33

Thanks: 10 times
Was thanked: 1 time(s) in 1 post(s)
I have to say I am not completely satisfied to have to do such a thing on my own and in such an eloborate fashion. However, I used your bitmapversion to implement an extension method to imagereader.

Thank you for your reply.
Users browsing this topic
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.