Opencl learning step by step (8) grayscale image histogram computing (2)

Source: Internet
Author: User

Now we use the method in the previous tutorial to count the number of pixels in a pair of rgba images (This pixel satisfies the arbitrary components of R, G, B, And a> = 5 )? The method I want to consider is to create a histogram of 256 bin. For a pixel, calculate max (R, G, B, A) and use this value to determine the pixel to enter the bin, after obtaining the histogram, the width * Height-hostbin [0]-hostbin [1]-hostbin [2]-hostbin [3]-hostbin [4], that is, the result we want.

The code in this tutorial is basically the same as that in the previous tutorial. The differences mainly include the following two points:

1. We have installed the rgba color chart.

// Load the image
Unsigned char * src_image = 0;
Gfreeimage IMG;
If (! IMG. LoadImage ("../lenna.jpg "))
{
Printf ("can't load lenna.jpg \ n ");
Exit (0 );
}
Src_image = IMG. getimagedata (width, height );

2. There is a slight change in the kernel code. The kernel code is as follows:

# Define linear_mem_access
# Pragma opencl extension cl_khr_byte_addressable_store: Enable

# Define binary size 256

/**
* Calculate the histogram. The value of bins is 256.
* Data Input Data
* Memory shared by all threads in a workgroup,
* Histogram of each workgroup
*/

_ KERNEL
Void histogram256 (_ global const uchar4 * data,
_ Local uchar * sharedarray,
_ Global uint * binresult)
{
Size_t localid = get_local_id (0 );
Size_t globalid = get_global_id (0 );
Size_t groupid = get_group_id (0 );
Size_t groupsize = get_local_size (0 );

// Initialize the shared memory
For (INT I = 0; I <bin_size; ++ I)
Sharedarray [localid * bin_size + I] = 0;

Barrier (clk_local_mem_fence );

Uchar R, G, B, A, T;
// Calculate the thread Histogram
For (INT I = 0; I <bin_size; ++ I)
{
# Ifdef linear_mem_access
R = (uint) data [groupid * groupsize * bin_size + I * groupsize + localid]. X;
G = (uint) data [groupid * groupsize * bin_size + I * groupsize + localid]. Y;
B = (uint) data [groupid * groupsize * bin_size + I * groupsize + localid]. Z;
A = (uint) data [groupid * groupsize * bin_size + I * groupsize + localid]. W;
Uint value = (uint) max (R, G), max (B, ));
# Else
Uint value = data [globalid * bin_size + I];
# Endif // linear_mem_access
Sharedarray [localid * bin_size + value] ++;
}

Barrier (clk_local_mem_fence );

// Merge the histograms of all threads in the workgroup to generate the workgroup histogram.
For (INT I = 0; I <bin_size/groupsize; ++ I)
{
Uint bincount = 0;
For (Int J = 0; j <groupsize; ++ J)
Bincount + = sharedarray [J * bin_size + I * groupsize + localid];

Binresult [groupid * bin_size + I * groupsize + localid] = bincount;
}
}

 

Complete code can be found:

Project File gcltutorial8

Download Code:

Http://files.cnblogs.com/mikewolf2002/gclTutorial.zip

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.