Getting started with image processing for cainiao

Source: Internet
Author: User
Tags bmp image

If this is the Getting Started, let's talk about the following parts: GDI and BMP.

1. GDI and BMP

The CDC in VC, that is, the device context is equivalent to graphics in C #, and there are methods such as lineto.
In fact, when we use graphics in C #, we are already using GDI +, but we are unaware of it.
So where is the GDI? Try to search the file named gdiplus or GDI32 on drive C. You should find it like this.
Delete directly should not be deleted, but you can change the name for him. Don't change it. You forgot O (partition _ partition) o ha !.
Then you can run a program like QQ swollen?
Initialization failure: 0x0000000e
Use the Image Viewer provided by Windows
When loading c: \ windows \ system32 \ shimgvw. dll, the system fails to find the specified file.
Therefore, GDI is everywhere in windows.
Do not think that bitmap is an image format, just like jpg gif. They are actually two completely different concepts.
In VC ++, cbitmap is the corresponding GDI data model, which is equivalent to bitmap in C #.
In this example, the. BMP bitmap file is a file representation of GDI. Bitmap files are not compressedAlgorithmOperations directly store Pixel matrix information, so the file size is very large.
The size of JPG files is very small. Why does JPG actually store the visual effects of Images Based on different algorithms and concepts?
It is mainly reflected in the brightness of two colors, the saturation of the color, that is, the color (the common point is the red, orange, yellow, green, blue, and purple), and the naked eye cannot reach the resolution capability of each pixel.
JPG is stored in this way.

2. Bitmap File Format

So let's talk about the BMP file format first. I am not the one that makes a long story.
The project's BINLOG contains a bitmap file named "BMP testimg.bmp. Open a hexadecimal editor:
Here is the download link

The bitmapheader defined in VC ++ is used to indicate that the Bitmap header information is not found in C.
In fact, it doesn't matter much. This is just a data organization method.
You can define such a structure if you want.
GDI has nothing to do with the device, but it does not mean that there is no association between the device and the computer that transmits an image file or image data is not a GDI object.
Microsoft helped us with this layer, that is, as long as it is connected to Windows devices, we can use
GDI displays the output on that device instead of the device itself. It can be said that the whole Windows provides us all the forms of GDI.
And so on. For example, when doing XX programming, You Need To directly manipulate the graphics card. In fact, this method is faster, but it is not necessary.

The so-called 24-bit true color mspaint painting new image default storage is 24-bit true color this is not an advanced technology because a pixel color with three eight
Is a 24-digit real color.
A true color image is capable of displaying 256x256x256 colors.
In addition, the default bitmap object created in C # is 24-bit true color, and the functions provided by graphic cannot operate non-true color bitmaps.

3. You can try again. Code \ (^ O ^ )/~

first, let's sell the example image you downloaded above. Do you see it? It's still dark. If you see it, you may see it again. Please visit chunge now.
you have recently studied DICOM and learned to drag the text. Hey,
can you describe it like this: BMP is a conventional and regular data organization method, regardless of whether it is in the memory or in the file, it is irrelevant to the platform with the specific programming language . unrelated
in BMP format, in short, the first 54 bytes of file header information are mainly the number of digits and width of the image, from 54 BITs, the color palette information is the pixel data without the color palette.
because this article does not specifically discuss the BMP file format, please refer to the BMP format
well, we will read this regular data: code for writing the first button event

Void BMP read () // read the BMP file in the format of {image BMP = (Bitmap) image. fromfile ("BMP testimg.bmp"); memorystream bmp data = new memorystream (); BMP. save (BMP data, imageformat. BMP); binaryreader BR = new binaryreader (BMP data ); // Why do we need to offset 18 bytes because the BMP format "" starts to store the image width and height BMP data with a 32-bit integer at the location of 18 bytes. seek (18, seekorigin. begin); int width = BR. readint32 (); int Height = BR. readint32 (); MessageBox. show (string. format ("width {0}, height {1}", width, height); // the starting position of the Data byte stored at 11th bytes. seek (10, seekorigin. begin); int datastart = BR. readint32 (); byte [] datas = new byte [width * Height]; int indx = 0; BMP data. seek (datastart, seekorigin. begin); // note that this is the position at the beginning of the color palette. Changing the color palette will make the "invisible" Image Display BMP data. seek (54, seekorigin. begin); random RD = new random (); BMP data. write (New byte [] {(byte) Rd. next (0,255), (byte) Rd. next (0,255), (byte) Rd. next (0,255), 0}, 0, 4); BMP data. write (New byte [] {(byte) Rd. next (0,255), (byte) Rd. next (0,255), (byte) Rd. next (0,255), 0}, 0, 3); image newbmp = bitmap. fromstream (BMP data); graphics. fromhwnd (this. handle ). drawimage (newbmp, new point (0, 0); BMP data. close (); BR. close ();}

The above code is very simple (⊙ o ⊙). You can understand it. Don't forget to put some pictures in the directory of the same level as the execution file.
This is a suitable O (strong _ strong) O for girlfriend!
A few notes
The two colors of BMP files are not necessarily black and white, right? They can be red green or the same colors, right?
Why does the storage size start at 19th bytes? Why is this BMP format ""? Ask Uncle gates.

When the "stream" Operation seek goes to the front and then the write operation is performed, will the data at the corresponding location be "squeezed" to the back?
No data stream is a type of "Overwrite" cursor type operation length will automatically identify the cursor to the farthest place file stream memory streams are the same
Therefore, if you want to insert data, merge files, or something like this, you have to get two data stream objects and exchange data to achieve the goal.
Again, he grabbed me.

Isn't it about reading data? It's about reading pixel value data.
Since it is to read the pixel value, we have to read one row and one row, just like scanning. Actually, it's stored in this way, but it's a little different.
That is to say, each line of image data is based on four bytes, which is not enough to be zero.
For example, if a row is scanned with 3 pixels, It is 9 bytes. If the number of 4 bytes is a multiple of 12 bytes, the remaining 3 bytes are all 0 bytes.
For example, if the scanning row has 20 pixels, it is 60 bytes. If the scanning row is a multiple of 4 bytes, it must have 60 bytes, because 60/4 is in addition to the net.

Let's Talk About This broken formula (width * 24 + 31)/32*4). I don't know which hot-headed person came up with it, note that 32 here refers to 32 bits, that is, 4 bytes.
In fact, I just want to say that the two words must have a deep understanding of the essence of data length calculation, I replaced the N statements above.
width is the image width 24, which represents the number of digits in each pixel. Calculate the actual number of bytes. Assume that the number of bytes is 31 after the first digit. Then, divide the Integer Data Division by 4 bytes to get a multiple of 4 bytes.
note that the final result is a scan row. the number of bytes
in this way, a row is scanned from left to right starting from the first point in the lower left corner of the image.
then, the BMP image pixel is stored in BGR order. instead of RGB, do not make a mistake,
in the past, setpixel () was used to process pixels. Now, I still write the code for setpix () second button:

Void setpix () // {filestream bmp data = file. open ("mm.bmp", filemode. open); binaryreader BR = new binaryreader (BMP data); BMP data. seek (10, seekorigin. begin); int BMP datastart = BR. readint32 (); BMP data. seek (18, seekorigin. begin); int width = BR. readint32 (); int Height = BR. readint32 (); bitmap newbmp = (Bitmap) New Bitmap (width, height, pixelformat. format24bpprgb); memorystream newbmp DATA = new memorystream (); newbmp. save (newbmp data, imageformat. BMP); binaryreader Br2 = new binaryreader (newbmp data); newbmp data. seek (10, seekorigin. begin); int newbmp datastart = br2.readint32 (); newbmp data. seek (newbmp datastart, seekorigin. begin); For (INT I = 0; I 

If you change for (INT I = 0; I From the above, we can see that in any environment, data is stored according to the same pattern, just like DICOM, data sharing can be achieved through this pattern as long as they follow this pattern.

It is said that the lockbitmap method is the fastest and indeed the fastest, because it uses pointers.
The following shows how to convert an image into a grayscale image. You can see that the code is much less and you don't have to worry about how to determine the index of each scan line. You can click it and then click it.
Note that the "Unsafe code allowed" option is selected in "Project-> property ":

 void lockpix () {bitmap BMP = (Bitmap) image. fromfile ("mm.bmp"); bitmapdata datas = BMP. lockbits (New rectangle (0, 0, BMP. width, BMP. height), system. drawing. imaging. imagelockmode. readwrite, system. drawing. imaging. pixelformat. format24bpprgb); unsafe {byte * P = (byte *) datas. scan0; int indx = 0; For (INT I = 0; I 
  

grayscale images are cool and earthy. They divide the three RGB values of each pixel by three. I don't want to tell you what I don't know.
but I still have. to tell you the so-called YUV representation method, Y represents the brightness. In this case, we have to talk about matrix multiplication.
what does this mean? Let's talk about matrix multiplication first
for example, the unit price of hats, shoes, and so in your store is expressed:
[25] [80] [15]
today, the hats sold three shoes and one so sold two shoes, which can be expressed:
[3]
[1]
[2]
today's revenue = 25x3 + 80x1 + 15x2 total 185 in a matrix, it is represented as [185].
I sold one shoes and three so the next day, then the sales for these two days can be expressed:
[3] [1]
[1] [2]
[2] [3]
what about the total revenue of the past two days = (25x3 + 80x1 + 15x2) + (25x1 + 80x2 + 15x3) A total of 185 + 230 = 415 represented in a matrix as [185] [230]
Yes, what you see is that matrix multiplication doesn't want to talk about linear algebra or anything else.

For example, in the preceding rgb yuv formula, three rows and three columns are multiplied by three rows and one column is three rows and one column,
The rule is that the columns of the first matrix are consistent with the rows of the second matrix to obtain a two-dimensional matrix of the columns at the end of the first row.
If the RGB values are {115,20, 65}, convert them to YUV, which indicates
Y = 115x0.299 + 20x0.587 + 65x0.114
U = 115x-0.148 + 20x-0.289 + 65x0.437
V = 115x0.615 + 20x-0.515 + 65x-0.1
It seems hard to understand because it is different from the previous one.
Turning the second matrix down to the left means alignment of its rows to the column of the First matrix is not much better!
If it's complicated, let's try again.
[2] [4] [-1] [6]
[1] [0] [3] [5]
What is the result?
2x-1 + 4x3 2x6 + 4x5
1x-1 + 0x3 1x6 + 0x5
Final Result
[10] [32]
[-1] [6]
You can also split it into a single row and a single column. It's much easier.
A special matrix is like a diagonal line.
Any matrix multiplied by him is equal to that matrix. It is a bit like "any number multiplied by 1 is equal to that number"
[1] [0]
[0] [1]

See, matrix multiplication is such a magic thing. Through matrix multiplication, angle rotation and scaling can also be performed.
This is a very advanced research topic! I will not discuss it here

I finally finished talking about it. I don't know why the following is a sample code for Matrix Multiplication. For details about the 5x5 matrix, refer to msdn:
Endless knowledge
Third button:

Void matrixcolor () {bitmap BMP = (Bitmap) image. fromfile ("mm.bmp"); imageattributes IA = new imageattributes (); // grayscale // float [] [] colormatrix = {// new float [] {0.299f, 0.299f, 0.299f, 0, 0}, // new float [] {0.587f, 0.587f, 0.587f, 0, 0}, // new float [] {0.114f, 0.114f, 0.114f, 0, 0}, // new float [] {0, 0, 0, 1, 0}, // new float [] {0, 0, 0, 0, 0, 1} // grayscale // float [] [] colormatrix = {// new float [] {0.3f, 0.3f, 0.3f, 0, 0 }, // new float [] {0.3f, 0.3f, 0.3f, 0, 0}, // new float [] {0.3f, 0.3f, 0.3f, 0, 0 }, // new float [] {0, 0, 0, 1, 0}, // new float [] {0, 0, 0, 0, 1 }//}; // reversed color // float [] [] colormatrix = {// new float [] {-1, 0, 0, 0, 0 }, // new float [] {0,-1, 0, 0, 0}, // new float [] {0, 0,-1, 0, 0 }, // new float [] {0, 0, 0, 1, 0}, // new float [] {1, 1, 1, 0, 1 }//}; // brightness float [] [] colormatrix = {New float [] {1, 0, 0, 0}, new float [] {0, 1, 0, 0, 0, 0}, new float [] {0, 0, 1, 0, 0}, new float [] {0, 0, 0, 1, 0 }, new float [] {l, 0, 1 }}; L-= 0.1f; colormatrix CM = new colormatrix (colormatrix); IA. setcolormatrix (CM, colormatrixflag. default, coloradjusttype. bitmap); graphics. fromhwnd (this. handle ). drawimage (BMP, new rectangle (0, 0, BMP. width, BMP. height), 0, 0, BMP. width, BMP. height, graphicsunit. pixel, Ia);} float L = 0.5f;

RGB is three energy values. We can see that the green color on the screen is different because the difference between the three energy values is that the ratio of the three values is different.
If the three values are the same it is similar to the light bulb, that is, the pure brightness, that is, the gray scale that we often say.
write a function for Manually changing the brightness of a very low efficiency.

Void light (ref int R, ref int g, ref int B) {// average after calculation // increase the brightness float gray = (R + G + B) + level * 90> 255*3? 255*3: (R + G + B) + level * 90; // brightness reduction // float gray = (R + G + B)-level * 90 <0? 0: (R + G + B)-level * 90; float percentr = (float) r/(R + G + B), percentg = (float) g/(R + G + B), percentb = (float) B/(R + G + B); r = (INT) (gray * percentr> 255? 255: gray * percentr); G = (INT) (gray * percentg> 255? 255: gray * percentg); B = (INT) (gray * percentb> 255? 255: gray * percentb); float Ren = gray-(R + G + B); If (REN> = 3) {r = (R + (INT) Ren)> 255? 255: (R + (INT) Ren); G = (G + (INT) Ren)> 255? 255: (G + (INT) Ren); B = (B + (INT) Ren)> 255? 255: (B + (INT) Ren) ;}} int level = 0;

In fact, you can simply multiply RGB by 1.2 or 1.1, but the color will be distorted.
Sample file and code

Now, I'm finished. So Tired, ya.

When it's done (⊙ o ⊙), it's really amazing. You know all the secrets, and you'll be confused when you get out.

Of course, fault tolerance is also very important as a commercial software code. You can see that ACDSee can still display some of the file data that you delete. Of course these are very simple.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.