C# Comparing images: Are the images' files the same - c#

I have seen only one solution for comparing images. But this solution checks if EVERYTHING about the images is the same. But I want to check if the images are loaded from the same image file from the resources.
I tried with
`"<object>.image == < image from the resources>"`
and
"<object>.Equals(<image from the resources>)"
but it didn't work. The way I know it "==" is used to checks if the two variables or objects are in the same place in the memory (are they the same thing) and "Equals" checks if the two variables or objects have the same values (correct me if I'm wrong). I have investigated why it always returns false.
So, is there a way to check if the images of two objects are loaded from the same image in the resources (are they using the same resource)?
If not, I'll have to make a new object or variable containing the image's name (that's the first thing that comes to my mind), because I want to compare only one thing about the images, not everything. If you have a better solution that you want to share with me, share it! I'll be happy to try a different algorithm.

you should use md5 int the equals function to quick check if its the same file....

Related

Powerpoint "Save As Picture" from C# Microsoft.Office.Interop.PowerPoint

My question is pretty similar to this one and I'm afraid the answer is the same... I want to save all the shapes/images on a slide as a single png (or jpeg). Programmatically, I get as far as
slide.Shapes.SelectAll();
but don't see a way to save as image. Is this possible? If not, any other suggestions, hopfully w/ examples? (not VBA - I need to automate the whole conversion)
There was a reference to OpenXML in the other post, but I'm not even sure how to pull that in.
I don't know how you'd do this in C# but I'd guess that you'd make use of the same methods as you would with VBA, where you can do:
Activewindow.Selection.ShapeRange.Export( "c:\temp\delete-me.jpg",ppShapeFormatJPG)
ppShapeFormatJPG is a PowerPoint constant, a VBA Long = 1; IIRC that'd be an Integer in C#.
The method also can take two more optional parameters, scalewidth and scaleheight, which govern the width and height of the exported image in undocumented ways. By default, no parms supplied, I get exports at 72 dpi. Larger numbers result in higher pixel count exports but distorted proportions. I'm sure there's some strange logic to it, but it escapes me; all hints welcome!
There's a third optional parm, ExportMode. In my tests, it makes no difference whether you supply it or not, and if you do, which of the available values you choose.

Getting a resource in Xamarin

I am trying to do something very basic: Have an ImageButton load an image and an associated sound and have that sound play. I thought it would be easy to do, but I'm having two problems (although it probably is two symptoms of the same problem).
One, I don't see the images I added to the Resources/drawable folder show up in the Resource.Drawable class, all I see there is the default Icon from icon.png. I added 20 images with a .jpg extension and set them to AndroidResource in properties. They were numerically named, so I modified them to start with a letter (in case that was the problem), got rid of a dot in the name (in case that was the problem), and finally renamed them to .jpeg (in case that was it). These don't seem to be reasons why they don't show up.
The Resource.Designer.cs works, though, because the two mp3s I added to the Resources/raw do show up in the class.
The other problem I have is that I can't tell how to load them programatically, so I can pass them to
imageButton.SetImageResource(imageId);
I tried
var imageId = this.Resources.GetIdentifier("DigiBord.Sunete:i00011", null, null);
which didn't work, but maybe the items were not in the project. So I tried again with an item I knew was in the project:
var imageId = this.Resources.GetIdentifier("DigiBord.Sunete:Icon", null, null);
which again yielded 0. There must be a way to load them without knowing the int id of the item ahead of time.

Quirky Directories

I've got an issue with some directory manipulation.
The problem is I have an archive of data that needs to be add or purge backup data based on a series of constraints. The constraint that is an issue is the archive only needs to keep the backup from the previous week.
So when you chart out the steps you would assume:
Check if directory exist.
Grab the files.
Then purge them.
Then move the following week into the directory.
The problem though is when you try keep the code simple and implementation, you create some code that doesn't feel like it is proper practice.
string[] archiveFiles = Directory.GetFiles(
Archive, #"*.*", SearchOption.TopDirectoryOnly);
foreach(string archive in archiveFiles)
File.Delete(archive);
So if you attempt to grab the files with Directory.GetFiles() and it doesn't return a value, according to the documentation:
Return Value Type: System.String[] An array of the full names
(including paths) for the files in the specified directory that match
the specified search pattern and option, or an empty array if no files
are found.
If it returns a null in the array then that would actually have the loop iterate once, an error. If it returns an array with no elements then it will ignore the loop. The second is what I believe it does, which makes this approach feel incorrect.
The only thing I could do would be to use File.Copy() as it can overwrite files, which would avoid this approach but even that could become suceiptable to the same dilemma of that empty array.
Is that right usage and approach for Directory.GetFiles() or is there a better way?
If it returns a null in the array then that would actually have the
loop iterate once, an error. If it returns an array with no elements
then it will ignore the loop. The second is what I believe it does,
which makes this approach feel incorrect.
If there are no files matching the list will be empty, there won't be nulls (how many nulls should return an empty directory?).
So your delete code will not be executed. Makes sense to me.
If you need to delete old files, then copy the new ones you may want to first move old files somewhere safe, then copy new ones, then to delete old files.
Maybe I didnt understand the problem here, but I don't see any. I hope actual code has some try catches though.

AddImageFilter error in SimpleITK

In a C# project, I ve got error using the AddImageFilter which is provided in the SimpleITK. Is there a common mistake that happens when trying to add two images with this filter? For example, maybe there is a rule the images should be both double or int.
The error I get is:
Image2 for AddImageFilter doesnt match type or dimension!
In a certain sense, your supposition is right. I couldn't been able to find the exact error you've got, but I found this sitkAddImageFilter implementation on GitHub. If you look at the AddImageFilter::Execute() function, at line 33, you'll find this exception been throwed:
std::cerr << "Both image for add filter don't match type or dimension!" << std::endl;
that seems related to yours (maybe the slight difference is just related to a different version of ITK). And that exception is throwed whenever this is verified:
if ( type != image2->GetDataType() || dimension != image2->GetDimension() )
So, a condition for the AddImageFilter is that both dimensions must be the same, and the metadata associated with the images must agree. This makes sense, because matrices addition is doable only when their dimensions match (and, of course, when they contain the same kind of informations).
If you are trying to add two different kinds of images (as an example: a DICOM with a TIFF), I suggest to convert at least one of them, in a way to have both of them in the same "metadata space".
The error message is a little vague and should be improved.
Many filters which take more than one image as input expect the following that pixel type, dimension, size, spacing and orientation to be same. The error message you got indicates that the pixel type or the size do not match. Likely emanating from the code generated from is line:
https://github.com/SimpleITK/SimpleITK/blob/master/TemplateComponents/ExecuteNoParameters.cxx.in#L8
I'd recommend printing your two images as strings to examine the meta-data to determine the difference.

Levenshtein (or Damerau-Levenshtein, if possible!) distance is Assembly

I have a program in which I need to calculate several times the Levenshtein distance between pairs of words (one of them fixed), and several times may range from about 1000 to 120000 for every fixed word. Since I want to optimize this program as much as I can I thought about implementing these calculations in assembly. The problem is that I know nothing about assembly except for the theory and that it may represent big speed improvements. Can anyone please help me or provide me with the assembly code for this distance? Also, how can I call assembly from a C# module?
You could easily use a BK-tree to create a lookup tree if Levenshtein is enough. Damarau-Levenshtein can not be used with a metric tree.
You dont need to write this implementation in assembler or C#, you can get far by using unsafe code and pointers.
Read and cache str.Length, those are method invocations (most probably inlined/optimized)
Access your strings with pointers.
fixed(char* ptrX=strX, ptrY=strY) ...
You can create your table/array/state as an int[rows*cols] instead of int[rows][cols] and use pointers to read/write.
int[] state = new int[rows*cols]
fixed(int* ptrState=state)
You dont really need more than two rows in your state table, you have the one you read from, and the one you write to. Then swap the pointers and read from what you just wrote.
I believe you can optimize by removing identical prefixes/suffixes
L('catz', 'cats') == L('z', 's') == 1
L('rats', 'cats') == L('r', 'c') == 1

Categories