Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. how to calculate all columns amount? [modified]

how to calculate all columns amount? [modified]

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialquestion
18 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G gentleguy

    dear all how to calculate each columns 255 value amount and then save a .txt file? the example is following: 0 0 0 0 0 0 0 0 0 255 255 255 255 255 0 0 255 255 255 255 255 0 0 255 255 255 255 255 0 0 255 255 255 255 255 0 0 255 255 255 255 255 0 0 0 0 0 0 0 for example: i want to get: 255 amount below: arryOne = columns 3 4 5 6 7 amount 5 5 5 4 4 how to get such the form of value? thanks a lot

    gentleguy

    modified on Thursday, June 26, 2008 5:29 AM

    S Offline
    S Offline
    Saurabh Garg
    wrote on last edited by
    #3

    Iterate over each column and count the number of times 255 occur. Isn't that simple? -Saurabh

    G 2 Replies Last reply
    0
    • R Rajesh R Subramanian

      What? :confused:

      Nobody can give you wiser advice than yourself. - Cicero .·´¯`·->Rajesh<-·´¯`·. Codeproject.com: Visual C++ MVP

      G Offline
      G Offline
      gentleguy
      wrote on last edited by
      #4

      thanks for your reply, sorry, the format changed there, actually i would like to calculate each column 255 amount, and then save an file: first row displays columns number and second row display amount of 255. however i revised the format,, it is still problem. do you understand me? thanks for your help in advance.

      gentleguy

      1 Reply Last reply
      0
      • G gentleguy

        dear all how to calculate each columns 255 value amount and then save a .txt file? the example is following: 0 0 0 0 0 0 0 0 0 255 255 255 255 255 0 0 255 255 255 255 255 0 0 255 255 255 255 255 0 0 255 255 255 255 255 0 0 255 255 255 255 255 0 0 0 0 0 0 0 for example: i want to get: 255 amount below: arryOne = columns 3 4 5 6 7 amount 5 5 5 4 4 how to get such the form of value? thanks a lot

        gentleguy

        modified on Thursday, June 26, 2008 5:29 AM

        I Offline
        I Offline
        Iain Clarke Warrior Programmer
        wrote on last edited by
        #5

        As has been said, just get on and count them. Is this data coming in from a text file? Then you can use CStdioFile to read it in one line at a time, and sscanf to process the rows - and maintain a count of number-of-255-s for each column. Is it in a 2 dimensional array already? Then use a loop, and count whether a number ==s 255 or not. If you have absolutely no idea, and this is homework (which I suspect, and is why I'm being unhelpful), then you will learn a lot more working with your fellow students and teacher than getting us to do the work. What is it you are struggling with? Iain.

        Plz sir... CPallini CPallini abuz drugz, plz plz help urgent.

        G 1 Reply Last reply
        0
        • S Saurabh Garg

          Iterate over each column and count the number of times 255 occur. Isn't that simple? -Saurabh

          G Offline
          G Offline
          gentleguy
          wrote on last edited by
          #6

          thanks for your reply, i am a newbie, could you give me detail or some code, thanks a lot.

          gentleguy

          1 Reply Last reply
          0
          • I Iain Clarke Warrior Programmer

            As has been said, just get on and count them. Is this data coming in from a text file? Then you can use CStdioFile to read it in one line at a time, and sscanf to process the rows - and maintain a count of number-of-255-s for each column. Is it in a 2 dimensional array already? Then use a loop, and count whether a number ==s 255 or not. If you have absolutely no idea, and this is homework (which I suspect, and is why I'm being unhelpful), then you will learn a lot more working with your fellow students and teacher than getting us to do the work. What is it you are struggling with? Iain.

            Plz sir... CPallini CPallini abuz drugz, plz plz help urgent.

            G Offline
            G Offline
            gentleguy
            wrote on last edited by
            #7

            thanks for your reply,actually i load one image which is binary image, actually the data is 2_D array. this is about my work...not homework, i am doing projection histogram, so i need to collect each column 255 amound. i am not good in c++..learning...so if possible, pls help me. thanks a lot

            gentleguy

            I 1 Reply Last reply
            0
            • S Saurabh Garg

              Iterate over each column and count the number of times 255 occur. Isn't that simple? -Saurabh

              G Offline
              G Offline
              gentleguy
              wrote on last edited by
              #8

              thanks for reply, for me maybe i have 400 or 700 hundrads columns,i load binary image, maybe 300x600 or evern big than this one... i am a newbie, can give me examlpe.thanks

              gentleguy

              S 1 Reply Last reply
              0
              • G gentleguy

                thanks for reply, for me maybe i have 400 or 700 hundrads columns,i load binary image, maybe 300x600 or evern big than this one... i am a newbie, can give me examlpe.thanks

                gentleguy

                S Offline
                S Offline
                Saurabh Garg
                wrote on last edited by
                #9

                Can you tell us how do you declare this 2D array? I will give you a hint for static 2D array in C++.

                int array[100][100];
                int count[100];       // There are 100 columns.
                
                for(int i=0 ; i<100 ; ++i)
                {
                    count[i] = 0;
                }
                
                for(int i=0 ; i<100 ; ++i)
                {
                    for(int j=0 ; j<100 ; ++j)
                    {
                        if(array[i][j] == 255)
                        {
                            count[j]++;
                        }
                    }
                }
                

                Hope this helps. -Saurabh

                G 1 Reply Last reply
                0
                • G gentleguy

                  thanks for your reply,actually i load one image which is binary image, actually the data is 2_D array. this is about my work...not homework, i am doing projection histogram, so i need to collect each column 255 amound. i am not good in c++..learning...so if possible, pls help me. thanks a lot

                  gentleguy

                  I Offline
                  I Offline
                  Iain Clarke Warrior Programmer
                  wrote on last edited by
                  #10

                  So, you have a block of bytes, and you know it is W wide, and H high. First - images often have the width padded by 4. Check whether this is the case or not.

                  void Count255s (BYTE *blob, UINT W, UINT H)
                  {
                  UINT nColumnCounts [W]; // W columns...
                  UINT x,y;

                  // clear counts.

                  = 0;

                  for (y = 0; y < H; y++)
                  {
                  for (x = 0; x < W; x++)
                  {
                  if (blob [x + y*W] == 255)

                  += 1;

                  }
                  }

                  // now we have counts!
                  DoSomethingwithCounts (W, nColumnCounts);
                  

                  }

                  There's no error checking, and there are more efficient ways of doing this, but this should be fairly clear for you, I hope. You could replace the check with:

                      if (\*blob++ == 255)
                  

                  to remove the expensive multiplication, but I'd avoid that unless you are very happy with pointers. Did that help at all? Iain.

                  Plz sir... CPallini CPallini abuz drugz, plz plz help urgent.

                  G 1 Reply Last reply
                  0
                  • I Iain Clarke Warrior Programmer

                    So, you have a block of bytes, and you know it is W wide, and H high. First - images often have the width padded by 4. Check whether this is the case or not.

                    void Count255s (BYTE *blob, UINT W, UINT H)
                    {
                    UINT nColumnCounts [W]; // W columns...
                    UINT x,y;

                    // clear counts.

                    = 0;

                    for (y = 0; y < H; y++)
                    {
                    for (x = 0; x < W; x++)
                    {
                    if (blob [x + y*W] == 255)

                    += 1;

                    }
                    }

                    // now we have counts!
                    DoSomethingwithCounts (W, nColumnCounts);
                    

                    }

                    There's no error checking, and there are more efficient ways of doing this, but this should be fairly clear for you, I hope. You could replace the check with:

                        if (\*blob++ == 255)
                    

                    to remove the expensive multiplication, but I'd avoid that unless you are very happy with pointers. Did that help at all? Iain.

                    Plz sir... CPallini CPallini abuz drugz, plz plz help urgent.

                    G Offline
                    G Offline
                    gentleguy
                    wrote on last edited by
                    #11

                    thanks a lot. very helpful, how can i save each column amount? thanks, sorry ask many by the way, why you use the third for loop? don't understnad.could you give me any explanation. thanks a lot

                    gentleguy

                    modified on Thursday, June 26, 2008 7:50 AM

                    I 1 Reply Last reply
                    0
                    • G gentleguy

                      thanks a lot. very helpful, how can i save each column amount? thanks, sorry ask many by the way, why you use the third for loop? don't understnad.could you give me any explanation. thanks a lot

                      gentleguy

                      modified on Thursday, June 26, 2008 7:50 AM

                      I Offline
                      I Offline
                      Iain Clarke Warrior Programmer
                      wrote on last edited by
                      #12

                      1st for loop is to zero out all the counts. I put the action bit on the same line as the loops, which might have been a bad idea... 2nd and 3rd loops are going through the x,y coords of your data. If you were just looking for the grand total of 255s, you could have done it in one. But as you need to keep track of which column you're on, you need a 2D loop pair. As for saving the column amount - however you like. If you just take my code and use it, you're in trouble. It's there to help you write your own. The answer to your question depends on your application, and how you put data into your histogram. You could create a CArray, and pass a pointer or reference to it to the count function, then the results would be available outside the function. Or you could have a CHistogram::InitialiseFromBitmapData function, and keep the results internally. Or... lots more choices. Iain.

                      Plz sir... CPallini CPallini abuz drugz, plz plz help urgent.

                      G 1 Reply Last reply
                      0
                      • I Iain Clarke Warrior Programmer

                        1st for loop is to zero out all the counts. I put the action bit on the same line as the loops, which might have been a bad idea... 2nd and 3rd loops are going through the x,y coords of your data. If you were just looking for the grand total of 255s, you could have done it in one. But as you need to keep track of which column you're on, you need a 2D loop pair. As for saving the column amount - however you like. If you just take my code and use it, you're in trouble. It's there to help you write your own. The answer to your question depends on your application, and how you put data into your histogram. You could create a CArray, and pass a pointer or reference to it to the count function, then the results would be available outside the function. Or you could have a CHistogram::InitialiseFromBitmapData function, and keep the results internally. Or... lots more choices. Iain.

                        Plz sir... CPallini CPallini abuz drugz, plz plz help urgent.

                        G Offline
                        G Offline
                        gentleguy
                        wrote on last edited by
                        #13

                        thanks a lot. here i will explain to you something, when i calculate 255 for each column, i just can start to scan my data(2-D) from left bottom, and then up row by row,do you understand me? thanks a lot, and disturb you a lot.

                        gentleguy

                        I 1 Reply Last reply
                        0
                        • G gentleguy

                          thanks a lot. here i will explain to you something, when i calculate 255 for each column, i just can start to scan my data(2-D) from left bottom, and then up row by row,do you understand me? thanks a lot, and disturb you a lot.

                          gentleguy

                          I Offline
                          I Offline
                          Iain Clarke Warrior Programmer
                          wrote on last edited by
                          #14

                          gentleguy wrote:

                          do you understand me?

                          Not really... The code I showed assumed that the data was row after row - I didn't assume whether I was starting with top left or bottom left - I've seen bitmap formats of both kind. If your data is stored differently, you can change the maths inside the [ and ] to look it up differently. Good luck, Iain.

                          Plz sir... CPallini CPallini abuz drugz, plz plz help urgent.

                          1 Reply Last reply
                          0
                          • G gentleguy

                            dear all how to calculate each columns 255 value amount and then save a .txt file? the example is following: 0 0 0 0 0 0 0 0 0 255 255 255 255 255 0 0 255 255 255 255 255 0 0 255 255 255 255 255 0 0 255 255 255 255 255 0 0 255 255 255 255 255 0 0 0 0 0 0 0 for example: i want to get: 255 amount below: arryOne = columns 3 4 5 6 7 amount 5 5 5 4 4 how to get such the form of value? thanks a lot

                            gentleguy

                            modified on Thursday, June 26, 2008 5:29 AM

                            H Offline
                            H Offline
                            Hamid Taebi
                            wrote on last edited by
                            #15

                            Can you use of Listctrl?

                            G 1 Reply Last reply
                            0
                            • S Saurabh Garg

                              Can you tell us how do you declare this 2D array? I will give you a hint for static 2D array in C++.

                              int array[100][100];
                              int count[100];       // There are 100 columns.
                              
                              for(int i=0 ; i<100 ; ++i)
                              {
                                  count[i] = 0;
                              }
                              
                              for(int i=0 ; i<100 ; ++i)
                              {
                                  for(int j=0 ; j<100 ; ++j)
                                  {
                                      if(array[i][j] == 255)
                                      {
                                          count[j]++;
                                      }
                                  }
                              }
                              

                              Hope this helps. -Saurabh

                              G Offline
                              G Offline
                              gentleguy
                              wrote on last edited by
                              #16

                              thanks for your help, my data is from binary image, different binary image has different dimension, when i load one 2-D binary image, depend on image size, but i can obtain width and height of image via access image definition. do you understand me? i can declare a 2-D dinamic array.thanks

                              unsigned char *ucImgdata1 =m_vicImg.ibuff;
                              int iWidth1 = m_vicImg.endx-m_vicImg.stx+1;
                              int iHeight1 = m_vicImg.endy-m_vicImg.sty+1;
                              int iWidthbyte1=m_vicImg.buffwidth;
                              int *nColumnCounts=new int[iWidth1];

                              for (int i=0;i<iwidth1;iwidth1;i++)
                              {
                              	nColumnCounts\[i\]=0;
                              }
                              for(int iY=0;iY<iheight;iheight1;iy++)
                              {
                              	for(int iX=0;iX<iwidth1;iwidth1;ix++)
                              	{
                              		
                              		if (imgArray \[iX+iY\*iwidth1\] == 255)          
                              		{
                              			
                              			nColumnCounts\[iX\]++;  
                              			
                              			FILE\* stream;
                              			stream=fopen("C:\\\\TestArray.txt","w");
                              			fprintf(stream," Array=%d\\n",nColumnCounts\[iX\]);
                              			fclose(stream);
                              		}
                              	}
                              

                              but i couldn't get the correct result. i would like to get one array including how many amount of 255 for each column.help me,thanks.

                              gentleguy

                              modified on Sunday, June 29, 2008 4:53 AM

                              G 1 Reply Last reply
                              0
                              • H Hamid Taebi

                                Can you use of Listctrl?

                                G Offline
                                G Offline
                                gentleguy
                                wrote on last edited by
                                #17

                                i am not sure for this. i tried many times, still couldn't finish it. do you have any other idea, thanks

                                gentleguy

                                1 Reply Last reply
                                0
                                • G gentleguy

                                  thanks for your help, my data is from binary image, different binary image has different dimension, when i load one 2-D binary image, depend on image size, but i can obtain width and height of image via access image definition. do you understand me? i can declare a 2-D dinamic array.thanks

                                  unsigned char *ucImgdata1 =m_vicImg.ibuff;
                                  int iWidth1 = m_vicImg.endx-m_vicImg.stx+1;
                                  int iHeight1 = m_vicImg.endy-m_vicImg.sty+1;
                                  int iWidthbyte1=m_vicImg.buffwidth;
                                  int *nColumnCounts=new int[iWidth1];

                                  for (int i=0;i<iwidth1;iwidth1;i++)
                                  {
                                  	nColumnCounts\[i\]=0;
                                  }
                                  for(int iY=0;iY<iheight;iheight1;iy++)
                                  {
                                  	for(int iX=0;iX<iwidth1;iwidth1;ix++)
                                  	{
                                  		
                                  		if (imgArray \[iX+iY\*iwidth1\] == 255)          
                                  		{
                                  			
                                  			nColumnCounts\[iX\]++;  
                                  			
                                  			FILE\* stream;
                                  			stream=fopen("C:\\\\TestArray.txt","w");
                                  			fprintf(stream," Array=%d\\n",nColumnCounts\[iX\]);
                                  			fclose(stream);
                                  		}
                                  	}
                                  

                                  but i couldn't get the correct result. i would like to get one array including how many amount of 255 for each column.help me,thanks.

                                  gentleguy

                                  modified on Sunday, June 29, 2008 4:53 AM

                                  G Offline
                                  G Offline
                                  gentleguy
                                  wrote on last edited by
                                  #18

                                  thanks a lot, i still get the correct result. but my data format is a little different. the detail is following:

                                  unsigned char *ucImgdata1 =m_vicImg.ibuff;
                                  int iWidth1 = m_vicImg.endx-m_vicImg.stx+1;
                                  int iHeight1 = m_vicImg.endy-m_vicImg.sty+1;
                                  int iWidthbyte1=m_vicImg.buffwidth;
                                  int *nColumnCounts=new int[iWidth1];
                                  for (int i=0;i<iwidth1;i++)
                                  { nColumnCounts[i]=0;
                                  }
                                  for(int iY=0;iY<iheight1;iy++)
                                  {
                                  for(int iX=0;iX<iwidth1;ix++)
                                  {
                                  if (imgArray [iX+iY*iWidth1] == 255)
                                  {
                                  nColumnCounts[iX]++;
                                  FILE* stream;
                                  stream=fopen("C:\\TestArray.txt","w");
                                  fprintf(stream," Array=%d\n",nColumnCounts[iX]);
                                  fclose(stream);
                                  }
                                  }
                                  }

                                  could help me? thanks

                                  gentleguy

                                  1 Reply Last reply
                                  0
                                  Reply
                                  • Reply as topic
                                  Log in to reply
                                  • Oldest to Newest
                                  • Newest to Oldest
                                  • Most Votes


                                  • Login

                                  • Don't have an account? Register

                                  • Login or register to search.
                                  • First post
                                    Last post
                                  0
                                  • Categories
                                  • Recent
                                  • Tags
                                  • Popular
                                  • World
                                  • Users
                                  • Groups