|
Hello,
I've created a function which walks pixel by pixel on an image, storing the pixel color on a Pixel Class.
The software was doing much processing on this loop because each time a new Pixel was found an instance of a Pixel Class was created. Then, I decided to declare a single Pixel Class instance outside the loop and then modify only its values inside the loop and then add it to the list of Pixel Class.
The problem started appearing after I did that. Now, when I try using BinarySearch on a List<t> it always returns me 0. It seems that C# is checking the Pixel Class instead of its content, which is what I want.
I tried to create an IComparer<t> to check the Pixel Class value but it doesn't work since the function always receive the same Class in both parameters, so the check is in vain.
So here I leave the question: How can I use BinarySearch on a class which only changes its content but not its address?
The (pseudo)code below illustrates the code before and after the changes:
BEFORE:
for (int x...)
for(int y...)
{
Pixel p = new Pixel();
p.value = ...
...
int pos = pixels.BinarySearch(p);
}
AFTER:
Pixel p = new Pixel();
PixelComparer pComp = new PixelComparer();
for (int x...)
for(int y...)
{
p.value = ...
...
int pos = pixels.BinarySearch(p);
int pos = pixels.BinarySearch(p, pComp);
}
public class PixelComparer : IComparer<Pixel>
{
public int Compare(Pixel a, Pixel b)
{
Console.WriteLine("{0} x {1}", a.value, b.value);
return a.value.CompareTo(b.value);
}
}
Thanks in advance,
|
|
|
|
|
Hi,
you can't store a lot of pixels in a single instance of Pixel; if you create only one Pixel, it will end up holding the data you inserted last. Storing a reference to the one Pixel over and over in some collection, does not help, all those references point to the same instance, so they all got changed to contain the same, last, value.
creating a new Pixel every time is too expensive as you discovered. So what you need to do is find a cheaper way for storing the data you want. I suggest an array of the simplest (value) type that can work for you, so either an array of int or an array of a little struct.
|
|
|
|
|
Hi Luc,
You are correct. I thought about that but my Pixel class contains two different fields:
1. Pixel color;
2. Number of hits (in order to track statistics of the color).
Since I sort the values in the end according to the number of hits I don't see an alternative but storing them together.
I've though about:
a. Changing the class to a structure to see if that speeds up the allocation time;
b. Using two different arrays but I think it will be hard to keep track of changes when they are rearranged.
c. Try to somehow retrieve the pallet of the image in beforehand and then compare it to the pixel color increasing only a hit vector where the hit position to be increased would be equivalent to the position of the pallet.
I think 'c' is the elegant solution but I don't know if it is the fast one. What do you think?
Regards,
|
|
|
|
|
OK, some comments:
1.
a general GDI+ image does not have a pallette, each pixel has ARGB values, typically 32-bit in total, and those can take any value. Your image source may be encoded by color index (e.g. 8-bit pallette) but AFAIK that is lost when the image gets loaded into an Image/Bitmap instance.
2.
with two arrays, their size would have to be the size (area) of the input image, that could be rather large. And you would have to implement your own sort algorithm to keep both arrays in sync.
3.
I would consider using a single Dictionary<Color,int> where the key is the pixel color and the value is the multiplicity. So the code to add a pixel would be: if already in dictionary increment value, else insert with value=1. Performing this on a megapixel image will be a slow operation!
BTW: make sure you allocate the dictionary with a well chosen initial capacity, I would pick image area divided by say 4 to avoid dictionary growth which would reallocate and copy all current data.
|
|
|
|
|
Hi Luc,
Reply to 1: Yes, I verified that just after I posted the reply.
Reply to 2: Agree.
Reply to 3: In fact, to use statistics I'd need a SortedDictionary since after I insert the values on this dictionary I need to sort them according to the number of hits. The problem here is that I verified that on SortedDictionary, as opposed to Dictionary, when you call Add method for an specific key, it does not update the value of the key, instead you have to remove the element using Remove and add it again using method. Therefore many search calls are done.
To illustrate:
if (color.ContainsKey(intColor))
{
int value = color[intColor] + 1;
color.Remove(intColor);
color.Add(intColor, value);
}
else
{
color.Add(intColor, 1);
}
Is there any other way to do that?
Regards,
|
|
|
|
|
|
No. You don't need a SortedList, as it does not make sense to get the order updated after every pixel, you only need it sorted when done collecting all info. And you want to sort by value, not by key.
Furthermore:
if (colorHistogram.ContainsKey(intColor)) colorHistogram[intColor]++;
else colorHistogram.Add(intColor, 1);
|
|
|
|
|
Hi all,
Can some one help me ?
where to post the questions related DOM and BHO that is on C#?
i never get answers for those questions.
can some one guide me for the proper place to post ! so that it cold be useful or me and others as well...
thanks
|
|
|
|
|
I'm developing a messenger like yahoo or windows live. I'm almost done and the way I choose is Web Service. One method receives byte array from user, some user info and saves in DB. Another method checks if any message available for that user if yes then return byte array. Its fine but I'm just confuse now that how to transfer a file. What I thought is that write chunk by chunk of file to server with last offset of original file. And when a chunk is read by other-end user the sender will write next part of file. So in that way transfer can be resume. But I'm wondering that is there any other better way to do that ?
TVMU^P[[IGIOQHG^JSH`A#@`RFJ\c^JPL>;"[,*/|+&WLEZGc`AFXc!L
%^]*IRXD#@GKCQ`R\^SF_WcHbORY87֦ʻ6ϣN8ȤBcRAV\Z^&SU~%CSWQ@#2
W_AD`EPABIKRDFVS)EVLQK)JKQUFK[M`UKs*$GwU#QDXBER@CBN%
R0~53%eYrd8mt^7Z6]iTF+(EWfJ9zaK-iTV.C\y<pjxsg-b$f4ia>
-----------------------------------------------
128 bit encrypted signature, crack if you can
|
|
|
|
|
Hey budy am not here to answer ur question. But to ask u something on the Messenger .........
I am also to develop an application similar to urs ..... I have been doing C# applications for 1 and half year ...... but I havent do any applications that include networking ............. So what is the general idea behind that ...... Or can u point me a site or notes which help me to do Messenger in C#.
Thank you.
|
|
|
|
|
I could collect all of features about any hardware but i could not obtain information about manufacture of RAM and MONITOR .
If it's possible help me about collectiong information about Ram and Monitore manufacture
|
|
|
|
|
Add a reference to Microsoft.VisualBasic; there's a ComputerInfo[^] class and a Computer[^] class that holds info on the screen
"Optimism is an occupational hazard of programming; feedback is the treatment."
-- Kent Beck
|
|
|
|
|
i ask this question in c# form body!
NOT VISUAL BASIC
|
|
|
|
|
moein.serpico wrote: i ask this question in c# form body!
NOT VISUAL BASIC
I think he knows that, so no need to shout. What he said was "Add a reference to Microsoft.VisualBasic; there's a ComputerInfo class and a Computer class" These are available to and fully usable in C#, but you have to add the reference to your project or VS can't find them. Why are they in Microsoft.VisualBasic.dll? Ask Mr Gates.
No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced.
This message is made of fully recyclable Zeros and Ones
|
|
|
|
|
You can try getting the required information using WMI[^].
Have a look at the Win32_PhysicalMemory[^] and Win32_DesktopMonitor[^] classes. They both contain an attribute for manufacturer name.
Here[^] and here[^] you can find some useful tutorials about using WMI in C#.
I hope this helps.
Regards
Nuri Ismail
|
|
|
|
|
I have made a simple game and for the name and score/top score, number of turns etc. of the players, I have used an xml file of my own.
Would I have any reason to store all that information in the .config file, making my own sections or key value pairs in appSettings.
Or is the .config file is only useful when we are talking configuration?
I have read many articles on codeproject/msdn etc., but still confused.
|
|
|
|
|
If you want to save the user data like High scores you can use xml.But if you want to use any application settings like path or something you are using in some code behind classes you can use configuration file.
For example if you have an upload path in server you can use appSettings.
|
|
|
|
|
humayunlalzad wrote: Would I have any reason to store all that information in the .config file
No. I always roll my own.
|
|
|
|
|
My personal preference for your problem is to use XML files, because I follow the simple rule,
'Use static configuration settings in App.Config file and Runtime settings in a separate file, preferably in XML format for easy reading and writing'.
|
|
|
|
|
Hi all,
i am working with webbrowser control.
is there any way to identify the network usage of IE.
for example:
from the time of IE open and till the end of IE Close,
just i want to calculate the entire network usage or page download and upload size(may be in bytes)
can we do it ondocument_complete event and ....
pls help me, if some one has any idea...
you can suggest some other idea also other than webbrowser
thanks in advance.
modified on Wednesday, September 9, 2009 5:31 AM
|
|
|
|
|
IE does not track these statistics.
You MAY be able to get some of this by using the Performance Counters, but I don't know if your going to be able to track it by application. I think you'll only get it on an Interface-by-Inerface basis. This will also pickup non-IE traffic and may affect the results you want.
AFAIK, the only way to track this would be to use a proxy server and point IE at it.
|
|
|
|
|
hi thank you for your suggestion
i am already using api hooking for this but my browser get hangs some time
so that i am trying to get it done in web browser itself and thank you for your suggestion if you come across any idea on that please let me know it
thank you
|
|
|
|
|
1) try to monitor the '80' port;
2) try to find the 'HttpAlicationRequest' object refer and so on, to get the stream.
3) if you want to get the 'web page' size, you can get the html text object to get it.
you can go to msdn to know more about webbrowser control.
modified 27-May-14 5:34am.
|
|
|
|
|
Hi all,
I am trying to crop an image based on a transparent panel that I have placed on the image. The transparent panel is placed on the image control based on certain coordinates (selected by a user based on mouse events). This works fine.
Now I want to try and crop the piece where the transparent panel is located on the image, but I keep receiving an exception: OutOfMemoryException => rect is outside of the source bitmap bounds.
But why is this happening? (note: The image Bounds value of X and Y is 0 and the transparent panel width and height is in bounds with the height and width of the image) The transparent panel is located within the picturebox, shouldn't it be able to get the correct position ??
Here is the code ...
...
TransparentPanel tmpPnl = e.ClickedItem.Tag as TransparentPanel;
Rectangle rec = tmpPnl.Bounds;
Bitmap bmpImage = new Bitmap(base.Image);
Bitmap bmpCrop = bmpImage.Clone(rec, bmpImage.PixelFormat);
...
FYI
That "out of memory" exception appears when you attempt to crop a piece of the image outside itself.
Eg: If the image is 800×600 and you go:
Bitmap b = CropBitmap(myPic, 700, 500, 200, 200);
That’ll try select from 700×500 to 900×700, which does not actually exist.
Many thanks in advance
Kind regards,
The only programmers that are better C# programmers, are those who look like this -> |
Programm3r
My Blog: ^_^
modified on Wednesday, September 9, 2009 6:01 AM
|
|
|
|
|
Ok, so the new method doesn't cause an exception.
But the new cropped image's position is a bit out. It crops more to the right. What am I doing wrong?
public static Bitmap CropBitmap(Bitmap srcBitmap, int cropX, int cropY, int cropWidth, int cropHeight)
{
Bitmap bmp = new Bitmap(cropWidth, cropHeight);
Graphics g = Graphics.FromImage(bmp);
g.DrawImage(srcBitmap, new Rectangle(0, 0, cropWidth, cropHeight), cropX, cropY, cropWidth, cropHeight, GraphicsUnit.Pixel);
g.Dispose();
return bmp;
}
Thanks again.
Kind regards,
The only programmers that are better C# programmers, are those who look like this -> |
Programm3r
My Blog: ^_^
|
|
|
|
|