[Opencv] image pixel randomization: snowflake flying

Source: Internet
Author: User

Recently, we often conduct experiments with the psychological department, and there are always various "What Is randomization? the physical nature of the stimulus remains the same .." . In the previous experiment of "masking", there was a set of images that were used to perform pixel randomization, which was the simplest randomization. At that time, there were only two kinds of pixels in the image, gray and dark gray, and there were few deep gray.

As a result, I calculated the number of gray-depth pixels, and then randomly spilled the gray-depth pixels in a gray image of the same size.

 

 
Int pix_count = 0; For (INT I = 0; I  (I ); for (Int J = 0; j  0) {int rand_x = rand () % pix_width; int rand_y = rand () % pix_width; uchar * p_pix = pix_masker.ptr <uchar> (rand_x); If (p_pix [rand_y * 3] = 128) {p_pix [rand_y * 3] = 78; p_pix [rand_y * 3 + 1] = 78; p_pix [rand_y * 3 + 2] = 78; pix_count --; // cout <pix_count <Endl ;}}

 

 

The general effect is as follows (* Ignoring a few letters overwrites the target stimulus effect ). However, this time, we need to randomize each frame of a stimulating video, and the video is colored. At the beginning, I thought that I would randomly find another vertex to exchange their RGB values for each vertex in the pixel.
 
Mat frame_copy (frame_rows, fram_cols, cv_8uc3, scalar (0, 0); frame. copyto (frame_copy); MAT frame_tag = mat: zeros (frame_rows, fram_cols, cv_8uc1); For (INT I = 0; I <frame_rows; I ++) {uchar * P = frame_copy.ptr <uchar> (I); For (Int J = 0; j <fram_cols; j ++ = 3) {uchar r = P [0]; uchar B = P [1]; uchar G = P [2]; int rand_row = rand () % frame_rows; int rand_col = rand () % fram_cols; // cout <rand_row <"" <rand_col <Endl; uchar * rand_p = frame_copy.ptr <uchar> (rand_row ); P [0] = rand_p [rand_col * 3 + 0]; P [1] = rand_p [rand_col * 3 + 1]; P [2] = rand_p [rand_col * 3 + 2]; rand_p [rand_col * 3 + 0] = r; rand_p [rand_col * 3 + 1] = B; rand_p [rand_col * 3 + 2] = g ;}}

But somehow, the effect is like this...

It's not because every vertex is random. In the future, it is randomly returned with a certain probability ??? So we changed the original scatter idea to create a black image of the same size, and read the pixel of the source image point by point. If a non-black point occurs, in the new graph, you can randomly find a black (that is, an unmodified vertex) and change the pixel value to the pixel value of this vertex in the source image. Complete Code :
Int main () {videocapture inputvideo ("sentiment learning. wmv"); If (! Inputvideo. isopened () {cout <"cocould not open the input video. "<Endl; Return-1;} size S = size (INT) inputvideo. get (cv_cap_prop_frame_width), // acquire input size (INT) inputvideo. get (cv_cap_prop_frame_height); int EX = static_cast <int> (inputvideo. get (cv_cap_prop_fourcc); // get codec type-int formvideowriter outputvideo; // open the outputoutputvideo. open ("Zhongxing. WMV ", cv_fourcc ('M', 'J', 'P ', 'G'), 30, S, true); srand (unsigned) Time (null); int frame_count = 0; while (true) {mat frame; inputvideo> frame; If (frame. empty () break; int frame_rows = frame. rows; int fram_cols = frame. cols; MAT frame_copy (frame_rows, fram_cols, cv_8uc3, scalar (0, 0); MAT frame_tag = mat: zeros (frame_rows, fram_cols, cv_8uc1 ); for (INT I = 0; I <frame_rows; I ++) {uchar * p_frame = frame. PTR <uchar> (I); For (Int J = 0; j <fram_cols; j + = 3) {uchar r = P_F Rame [J + 0]; uchar B = p_frame [J + 1]; uchar G = p_frame [J + 2]; If (r> 0) | (B> 0) | (G> 0) {bool if_tag = false; while (! If_tag) {int rand_row = rand () % frame_rows; int rand_col = rand () % fram_cols; uchar * p_tag = frame_tag.ptr <uchar> (rand_row ); uchar * p_copy = frame_copy.ptr <uchar> (rand_row); If (p_tag [rand_col] = 1) continue; else {p_tag [rand_col] = 1; p_copy [rand_col * 3 + 0] = r; p_copy [rand_col * 3 + 1] = B; p_copy [rand_col * 3 + 2] = g; if_tag = true ;}}}}cout <"Write frame:" <frame_count ++ <Endl; outputvideo <frame_copy ;} cout <"finished writing" <Endl; return 0 ;}

Then we realized the effect of random snowflake flying in the sky ~(Reprinted please indicate the author and Source: http://blog.csdn.net/xiaowei_cqu is not allowed for commercial use)

 

 

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.