how to change the black pixels in the below image to red pixels using opencv libraries with c++ -


ball

i want change black pixels in image red pixels, such ball should white , red. want use opencv libraries , code in c++. have tried converting image rgb.

common approach threshold image, in case each pixel intensity less threshold considered being black , recolored red. 1 way find threshold (that divides image's pixel 2 classes ("more black" , "more white") otsu thresholding:

int main() {     cv::mat input = cv::imread("../inputdata/ball_thresholding.jpg");      cv::mat gray;     cv::cvtcolor(input,gray,cv_bgr2gray);      cv::mat mask;     // compute inverse thresholding (dark areas become "active" pixel in mask) otsu thresholding:     double graythres = cv::threshold(gray, mask, 0, 255, cv_thresh_binary_inv | cv_thresh_otsu);      // color masked pixel red:     input.setto(cv::scalar(0,0,255), mask);      // compute median filter remove whitish black parts , darker white parts      cv::imshow("input", input);     cv::waitkey(0);     return 0; } 

giving mask:

enter image description here

and result:

enter image description here

for image, threshold computed otsu 127, means each grayscale pixel intensity of 127 or less (or less 127, i'm not sure) recolored red.

if want keep shading effect withing black/red region, can remove input.setto(cv::scalar(0,0,255), mask); lind , replace by:

// keep shading:     for(int j=0; j<input.rows; ++j)         for(int i=0; i<input.cols; ++i)         {             if(mask.at<unsigned char>(j,i))             {                 input.at<cv::vec3b>(j,i)[2] = 255;             }         } 

which result int:

enter image description here


Comments

Popular posts from this blog

android - Why am I getting the message 'Youractivity.java is not an activity subclass or alias' -

python - How do I create a list index that loops through integers in another list -

c# - “System.Security.Cryptography.CryptographicException: Keyset does not exist” when reading private key from remote machine -