Several issues worth attention in Array Processing of One-Dimensional Image Data

Source: Internet
Author: User

Here, we will give a few simple examples of image processing to see how to use the two functions we have just written and the several issues that should be paid attention.

1. Reversed image color

The reversed color of the image changes from black to white, and from white to black. The algorithm is very simple. newpixel = 255-oldpixel.

We try to write a program, as shown in the following figure. timeelapse records the execution time of the program, in the unit of haoss.

Public shared function invert (byval BMP as bitmap, byref timeelapse as integer) as Boolean
Timeelapse = environment. tickcount
Dim BMP data () as byte, I as integer
Readbitmap (BMP, BMP data) 'reads image data
Dim number as integer = BMP data. Length-1
For I = 0 to number
BMP data (I) = 255-BMP data (I) 'reversed
Next
Writebitmap (BMP, BMP data) 'writes data
Timeelapse = environment. tickcount-timeelapse
Return true
End Function

At first glance, there was no problem with the above Code and the results were correct. In fact, there was a conceptual error. Not all data in BMP data is image data. For a 50*60 real-color image, BMP data. length is not 50*3*60, but (50*3 + 2) * 60. In fact, in the readbitmap function, the length of BMP data is defined as BMP. height * stride-1, where stride is used instead of BMP. width * 3. This is because windwos requires that the length of a row to be scanned must be a multiple of 4 (in bytes). If not, it must be completed. Calculation formula: stride = (BMP. Width * 24) + 31)/32) * 4. You can try this value in readbitmap and check whether it is consistent with data. stride. Therefore, the above process should be written as follows:

Public shared function invert (byval BMP as bitmap, byref timeelapse as integer) as Boolean
Timeelapse = environment. tickcount
Dim BMP data () as byte, I, j, stride, temp as integer
Readbitmap (BMP, BMP data) 'reads data
Stride = (BMP. Width * 24) + 31)/32) * 4' calculate a scan row
For I = 0 to BMP. Height-1
For J = 0 to BMP. Width-1
Temp = I * stride + J * 3 'Avoid unnecessary intermediate Variables
BMP data (temp) = 255-BMP data (temp) 'here do not use the for k = 0 to 2 loop, the efficiency will be much reduced
BMP data (temp + 1) = 255-BMP data (temp + 1)
BMP data (temp + 2) = 255-BMP data (temp + 2)
Next
Next
Writebitmap (BMP, BMP data) 'writes data
Timeelapse = environment. tickcount-timeelapse 'program time
Return true
End Function

The above process introduces some additional computation because of two repeated loops, so the speed is about 10% slower than that of the first function, but the speed is still quite fast, it takes about 40 ms for an image of 1024*768 (if no special instructions are added, it will take about 40 ms for later time consumption to be calculated based on the image size) (what is impossible, check whether you have configured the optimization properties. Select the menu item> XX Properties> Configuration Properties> optimization> check to enable optimization and remove integer overflow ). For the simple processing of a single pixel like reversed color, if you are purely pursuing speed, you can use the first function, but it is not recommended because once it involves domain processing, that won't work (unless your image width is exactly an integer multiple of 4 ).

2. convert a color image to a gray image

Before I realized this problem, I wrote a grayscale processing program:

Public shared function grayscale (byval BMP as bitmap, byref timeelapse as integer) as Boolean
Timeelapse = environment. tickcount
Dim I as integer
Dim BMP data (), grayvalue as byte
Readbitmap (BMP, BMP data)
Dim number as integer = BMP data. Length-1
For I = 0 to number step 3
Grayvalue = BMP data (I) * 0.114 + BMP data (I + 1) * 0.587 + BMP data (I + 2) * 0.299 'grayscale Value
BMP data (I) = grayvalue
BMP data (I + 1) = grayvalue
BMP data (I + 2) = grayvalue
Next
Writebitmap (BMP, BMP data)
Timeelapse = environment. tickcount-timeelapse
Return true
End Function
I also wrote a program to subtract two images, and found that the image generated by the Code is not exactly the same as the grayscale image written in the previous version 6.0. It is normal to change it to the following format:

Public shared function grayscale (byval BMP as bitmap, byref timeelapse as integer) as Boolean
Timeelapse = environment. tickcount
Dim BMP data (), grayvalue as byte
Dim I, j, temp, stride as integer
Stride = (BMP. Width * 24) + 31)/32) * 4
Readbitmap (BMP, BMP data)
For I = 0 to BMP. Height-1
For J = 0 to BMP. Width-1
Temp = I * stride + J * 3
Grayvalue = BMP data (temp) * 0.299 + BMP data (temp + 1) * 0.587 + BMP data (temp + 2) * 0.114
BMP data (temp) = grayvalue
BMP data (temp + 1) = grayvalue
BMP data (temp + 2) = grayvalue
Next
Next
Writebitmap (BMP, BMP data)
Timeelapse = environment. tickcount-timeelapse
Return true
End Function

The above process takes about Ms.

3. Delete red

To delete a color, set the color weight to 0. Here, the red color is deleted as an example. The following code is used:

Public shared function killred (byval BMP as bitmap, byref timeelapse as integer) as Boolean
Timeelapse = environment. tickcount
Dim I, j, stride as integer
Dim BMP data () as byte
Readbitmap (BMP, BMP data)
Stride = (BMP. Width * 24) + 31)/32) * 4
For I = 0 to BMP. Height-1
For J = 0 to BMP. Width-1
BMP data (I * stride + J * 3) = 0
Next
Next
Writebitmap (BMP, BMP data)
Timeelapse = environment. tickcount-timeelapse
Return true
End Function

Select an image with a red area. After the above changes, the original red area is still reddish, where is the problem? You can use a color filter tool or the getpixel function to obtain the color component of a certain vertex. If you compare the color component with the array in your array, you will find the problem, in the past, the location of image data in GDI + was not our common RGB sequence, but BGR. In this way, the aforementioned process of BMP data (I * stride + J * 3) change = 0 to BMP data (I * stride + J * 3 + 2) = 0. Everything will be quiet !!

 

 

 

 

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.