By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,985 Members | 1,830 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,985 IT Pros & Developers. It's quick & easy.

How to remove one color in a image.

P: n/a
Hi guy's

I am working on a sprite game in c++ and OpenCV. I have created to images, the backgroundimage (the background image in the game and the spriteman (my character in the game).

How do I remove the colour blue colour around my character?

Expand|Select|Wrap|Line Numbers
  1. int main() {
  2.  
  3. // Loading Images - Loading the Background Image
  4. IplImage* backgroundimage = cvLoadImage("mission11.png"); 
  5. backgroundimage->width;
  6. backgroundimage->height;
  7.  
  8. // Creating an empty images - Setting the widt and height values as backgroundimage
  9. IplImage* temp = cvCreateImage(cvSize(backgroundimage->width,backgroundimage->height),IPL_DEPTH_8U,3) ;
  10.  
  11. // Loading Images - Loading the Sprite Image
  12. IplImage* spriteman = cvLoadImage("spriteman.jpg"); //change the file name with your own image
  13. spriteman->width;
  14. spriteman->height;
  15.  
  16. // The start place of the sprite 
  17. int placeY = 80;
  18. int placeX = 80;
  19.  
  20. // Creating a variable for the keys ind the game
  21. char presskey;
  22.  
  23. // The start place of the sprite
  24. bool run = true;
  25.  
  26. while(run){
  27.  
  28.     char presskey = cvWaitKey(5);
  29.  
  30.     switch(presskey) {
  31.     case 'w':
  32.     case 38:
  33.         placeY -=10;
  34.         break;
  35.     case 'a':
  36.     case 39:
  37.         placeX -=10;
  38.         break;
  39.     case'd':
  40.     case 40:
  41.         placeX +=10;
  42.         break;
  43.     case's':
  44.     case 41:
  45.         placeY +=10;
  46.         break;
  47.     case 32:
  48.         placeY -=20;
  49.         break;
  50.     case'q':
  51.     case 27:
  52.         run=false;
  53.         break;
  54.     }
  55.  
  56.     for(int i=0;i<backgroundimage->height*backgroundimage->width*backgroundimage->nChannels;i++){
  57.         temp->imageData[i]=backgroundimage->imageData[i];
  58.     }
  59.     for(int y = 0; y < spriteman->height; y++){
  60.         for(int x = 0; x < spriteman->width; x++){
  61.             for(int z = 0; z < spriteman->nChannels; z++){
  62.                 if(((uchar *)(spriteman->imageData + y*spriteman->widthStep))[x*spriteman->nChannels + z] < 260){
  63.                     temp->imageData[(y + placeY) * backgroundimage->widthStep + (x+placeX) * backgroundimage->nChannels + z] = spriteman->imageData[y * spriteman->widthStep + x * spriteman->nChannels + z];
  64.  
  65.                 }
  66.  
  67.             }
  68.         }
  69.     }
  70.  
  71.     // Display the image temp
  72.     cvShowImage("Metal Slug: Kill Rambo", temp);
  73. }
  74.  
  75. //create a new window with  
  76. cvNamedWindow("Metal Slug: Kill Rambo", CV_WINDOW_FULLSCREEN);
  77.  
  78.  
  79. //Destroying the Window and the Images within
  80. cvDestroyWindow( "Metal Slug: Kill Rambo" );
  81. cvReleaseImage( &backgroundimage );
  82. cvReleaseImage( &temp );
  83. cvReleaseImage( &spriteman );
  84. return 0;
  85.  
  86. }
  87.  
Oct 11 '10 #1
Share this Question
Share on Google+
1 Reply


P: 2
Hi,
You are using a png image, so i assume that u r image is 32-bit and ARGB pixel format.

All you have to do is check for each pixel color and if it matches 0xff0000ff than replace it with 0x00000000, so that blue color in the image vil become transparent.

To get more help on web search for color keying algorithm.

Hope this helps.
Oct 12 '10 #2

Post your reply

Sign in to post your reply or Sign up for a free account.