Click here to Skip to main content
15,922,574 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello Friends,
I am Shailendra
We want to take an image that is looking at a rectangle from below (creating a trapezoid) and change it into the original rectangle. For example, imagine you were on the ground taking a photograph of a skyscraper. I want to use that data to find what the side of the skyscraper looks like. How can I do that?
Keystone Correction normally refers to an offset projector, but that's not exactly what's happening. My source image is already messed up in the same way as an offset projector would make it. I need to get it straight-on.
We are using CP PLUS IP Camera and we are working in C# dot net application.
Our requirement is to correct image at live view of camera.

Please guide for above problem’s solution.

I have search about it and found following code but its not complete code.
so if any one have complete code then please.

Thank you

What I have tried:

C#
public class LockBitmap
      {
          Bitmap source = null;
          IntPtr Iptr = IntPtr.Zero;
          System.Drawing.Imaging.BitmapData bitmapData = null;

          public byte[] Pixels { get; set; }
          public int Depth { get; private set; }
          public int Width { get; private set; }
          public int Height { get; private set; }


          public void LockBitmap(Bitmap source)
          {
              this.source = source;
          }
          /// <summary>
          /// Lock bitmap data
          /// </summary>
          public void LockBits()
          {
              try
              {
                  // Get width and height of bitmap
                  Width = source.Width;
                  Height = source.Height;

                  // get total locked pixels count
                  int PixelCount = Width * Height;

                  // Create rectangle to lock
                  Rectangle rect = new Rectangle(0, 0, Width, Height);

                  // get source bitmap pixel format size
                  Depth = System.Drawing.Bitmap.GetPixelFormatSize(source.PixelFormat);

                  // Check if bpp (Bits Per Pixel) is 8, 24, or 32
                  if (Depth != 8 && Depth != 24 && Depth != 32)
                  {
                      throw new ArgumentException("Only 8, 24 and 32 bpp images are supported.");
                  }

                  // Lock bitmap and return bitmap data
                  bitmapData = source.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite,
                                               source.PixelFormat);

                  // create byte array to copy pixel values
                  int step = Depth / 8;
                  Pixels = new byte[PixelCount * step];
                  Iptr = bitmapData.Scan0;

                  // Copy data from pointer to array
                  Marshal.Copy(Iptr, Pixels, 0, Pixels.Length);
              }
              catch (Exception ex)
              {
                  throw ex;
              }
          }

          /// <summary>
          /// Unlock bitmap data
          /// </summary>
          public void UnlockBits()
          {
              try
              {
                  // Copy data from byte array to pointer
                  Marshal.Copy(Pixels, 0, Iptr, Pixels.Length);

                  // Unlock bitmap data
                  source.UnlockBits(bitmapData);
              }
              catch (Exception ex)
              {
                  throw ex;
              }
          }

          /// <summary>
          /// Get the color of the specified pixel
          /// </summary>
          /// <param name="x"></param>
          /// <param name="y"></param>
          /// <returns></returns>
          public Color GetPixel(int x, int y)
          {
              Color clr = Color.Empty;

              // Get color components count
              int cCount = Depth / 8;

              // Get start index of the specified pixel
              int i = ((y * Width) + x) * cCount;

              if (i > Pixels.Length - cCount)
                  throw new IndexOutOfRangeException();

              if (Depth == 32) // For 32 bpp get Red, Green, Blue and Alpha
              {
                  byte b = Pixels[i];
                  byte g = Pixels[i + 1];
                  byte r = Pixels[i + 2];
                  byte a = Pixels[i + 3]; // a
                  clr = Color.FromArgb(a, r, g, b);
              }
              if (Depth == 24) // For 24 bpp get Red, Green and Blue
              {
                  byte b = Pixels[i];
                  byte g = Pixels[i + 1];
                  byte r = Pixels[i + 2];
                  clr = Color.FromArgb(r, g, b);
              }
              if (Depth == 8)
              // For 8 bpp get color value (Red, Green and Blue values are the same)
              {
                  byte c = Pixels[i];
                  clr = Color.FromArgb(c, c, c);
              }
              return clr;
          }

          /// <summary>
          /// Set the color of the specified pixel
          /// </summary>
          /// <param name="x"></param>
          /// <param name="y"></param>
          /// <param name="color"></param>
          public void SetPixel(int x, int y, Color color)
          {
              // Get color components count
              int cCount = Depth / 8;

              // Get start index of the specified pixel
              int i = ((y * Width) + x) * cCount;

              if (Depth == 32) // For 32 bpp set Red, Green, Blue and Alpha
              {
                  Pixels[i] = color.B;
                  Pixels[i + 1] = color.G;
                  Pixels[i + 2] = color.R;
                  Pixels[i + 3] = color.A;
              }
              if (Depth == 24) // For 24 bpp set Red, Green and Blue
              {
                  Pixels[i] = color.B;
                  Pixels[i + 1] = color.G;
                  Pixels[i + 2] = color.R;
              }
              if (Depth == 8)
              // For 8 bpp set color value (Red, Green and Blue values are the same)
              {
                  Pixels[i] = color.B;
              }
          }
      }
Posted
Updated 7-Jun-23 3:21am
v2

1 solution

While we are more than willing to help those that are stuck, that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

And "I found this code but it doesn't do what I want" doesn't count as "trying".

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.

If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900