EmguCV: Emplace one Mat onto another? - c#

In C++ if you wanted to emplace one mat into another, the code would simply be:
clone_img.emplace(out_mat);
Now, I know in C# there is no real equivalent to the "emplace" method. If I was working with other data types in a more standard collection, I can find the answer for what I want to do on Google. Working with a cv::Mat is a bit different though, and I can't find anything about that.
There is a Mat.PushBack method, but it doesn't construct the new object in-place within the collection. Or does that not matter?
What is the C#/EmguCV equivalent of this emplacement, and what would be the differences under the hood to be aware of?

I found the answer for what I wanted to do, although it may not exactly answer this above question.
What I wanted to do was to create an empty Mat, and then call Mat.emplace() to put something else in it.
I found out it was as simple as doing:
Mat newMat = old_mat;

Related

How do I know if certain extensions methods 'mutate' the object or not?

Feel free to edit 'mutate' from the title if it's a poor choice of wording.
My question is, relatively, simple.
Take the following example:
myCollection.OrderBy(o => o);
How do I know whether OrderBy will/will not order myCollection or whether an assignment (following) is necessary:
myCollection = myCollection.OrderBy(o => o);
Is it a case of having to build it and check every time I encounter an extension I'm unfamiliar with?
Please note: I'm not asking whether this will or will not affect myCollection, I already know the answer to that from using it hundreds of times previous, I'm asking how I'd know.
You can't tell from just the signature.
The best you can do is to investigate the actual code, for example by looking at the .NET Reference Source. Another thing you could do is check the return type. If it is the same as the one it's being called on, it probably doesn't change it, it most likely returns a new instance. Is it a void, then it probably does change something inside.
For your specific case for example, OrderBy: no. See here. It 'just' returns a new OrderedEnumerable.
You can check for the Pure attribute in the class decoration as Steven Liekens said. But in its absence, the only way to know for sure is by:
Experimenting: for example, get an instance of the class and serialize it. Use the method and then serialize it. Compare the results. May not be accurate every time.
Reverse engineering the method: and I hope you have the source code. If you don't, you can use reflection. This will require some judgement if the method is somewhat complex, but this complexity here is subjective.
Reading the docs and trusting them - if the doc is present. This is the sensible thing to do with the .NET Framework types, and an exercize of faith otherwise.
One way is to find out if the method or its class is marked as [Pure]. Pure code does not modify input values.

Check if a Bitmap has been modified

I have a class that wraps around a Bitmap and I would like to have a way of knowing if the bitmap has been changed (via SetPixel or GDI+).
I don't need to know exactly when it happens, I just need a way to tell if it has happened since the last check.
Now, I'm assuming that something like that isn't already packed in the Bitmap class, so what would be the best way to solve this problem?
I could provide my own wrapper functions for GetPixel and SetPixel, but then I'd have no idea if the Bitmap was changed using GDI. I COULD make a wrapper for that too but that really seems like a huge overkill.
Another possible option would be to save a copy and then check pixel by pixel. This would obviously work and would be trivial to write but it's much too slow for my needs.
You should use a hash or check sum. There are some easy ways to do this but I think the simplest way would be to have a string hash property in your class then call GetHashCode() on the bitmap/binary string/whatever container you're storing it in. Set it to the classes property, check the current hashcode against that value to see if anything has changed. You could also write your own little checksum function or choose from (I'm sure) a vast array of third party options.

Converting C# to Java, how do I handle IEnumerable?

I've been tasked with converting some C# code to Java. Most of it is fine, but I am having some trouble working out how to translate IEnumerable.
The code I have in C# is this:
public IEnumerable<Cat> Reorder(IEnumerable<Cat> catList)
{
// some logic that reorders the list
}
My googling suggested that I should be using Iterable<Cat> as an alternative. However, I also stumbled upon something saying you should never have Iterable<T> as a return type.
I'm a bit unfamiliar with data structures in Java. What should I be returning, and how would you re-order a collection of objects?
In C#, assuming you don't use linq, you'd create an empty array or List or similar, and add the items in as you repeatedly iterate through them, checking the criteria. Which data structure would I use in Java to achieve this?
It depends a bit on what you want to do with the return value.
Java has no LINQ, so using an Iterable<T> other than inside a foreach loop is a bit of a PITA. This blog post describes it in more depth.
The alternative is to return a Collection<T>.
Having said that, returning an Iterable<T> is not wrong, it just makes it harder to consume the return value in certain scenarios.
In Java you would use an implementation of List<T> like ArrayList<T> for temporary instances inside methods. When you would want to return that instance from a method, the return type would be the interface List<T> or Collection<T>.
You could do something like Collections.sort(list); if you implement a Interface Comparable at your objects (similiar can be done with c# and the IComparer)
"add the items in as you repeatedly iterate through them," I hope you don't really mean what I'm thinking you mean... There are a hell lot of sorting algorithms.
There's nothing wrong with Iterable, when you need to use it.
However, considering what the code above tells me, I think you'd be better going with java.util.Collection or java.util.List (the latter if catList is definitely a list of cats).
The way of reordering actually depends on the ordering requirements. It's probably as simple as using Collections.sort(List list)
You should identify the data structure you need.
Basically the 3 great data structure families are:
List: An ordered index-based collection;
Set: A collection that contains no duplicate elements;
Map: A key-value based collection (A dictionnary in C#). Not adapted here in this precise case.
Depending on what you need, you should return List<Cat> or Set<Cat>.

From .Net to Java

Which would be the best way to write the same code in Java?:
Array.Resize(ref DCamposGraficoOperaciones[index].DatosMeses, 12);
I have this code in C# and I have to put it on in Java. Thanks so much. I have a method called resizeDatosMeses in Java to resize the array but when I try to do it in this way:
DCamposGraficoOperaciones[index].getDatosMeses()=resizeDatosMeses(DCamposGraficoOperaciones[index].getDatosMeses(), 12)
I have a mistake which is: The left-hand side of an assignment must be a variable, please could you advice me?
Thanks so much
I suspect you want:
DCamposGraficoOperaciones[index].setDatosMeses(
resizeDatosMeses(DCamposGraficoOperaciones[index].getDatosMeses(), 12));
You can't assign to the result of a method call, which is what you were trying to do before.
I would strongly encourage you to break this line into two separate statements - and also start following Java naming conventions, which would prohibit DCamposGraficoOperaciones as a variable name.
Also, it's not clear why you're resizing an array to start with, but in both .NET and Java you may well be better off using a higher-level abstraction, e.g. List<T> in .NET or ArrayList<T> in Java.
In java you cant re-size arrays. You can use ArrayList and you can add any number of values to it.

What is the best way to convert between a Media.Matrix to a Drawing2D.Matrix

I have the need to convert from the WPF Media.Matrix to the Windows Forms Drawing2D.Matrix and so I did the following:
public static System.Drawing.Drawing2D.Matrix ConvertToDrawing2DMatrix( Matrix matrix)
{
return new System.Drawing.Drawing2D.Matrix((float)matrix.M11,
(float)matrix.M12,
(float)matrix.M21,
(float)matrix.M22,
(float)matrix.OffsetX,
(float)matrix.OffsetY);
}
and was wondering if this was the best approach.
If your code works fine then I would say that probably is the best method in your case. I looked all over google trying to find a way to do this and other than a 3rd party library I couldn't seem to find a way.
As others have said that is probably the best way. I just wanted to add that depending on the .NET version and your coding policy you could consider adding "this" to the method signature and make it an extension method for easier access, like this:
using Drawing2DMatrix = System.Drawing.Drawing2D.Matrix;
public static Drawing2DMatrix ConvertToDrawing2DMatrix(this Matrix matrix) {...}
Then you could call it like this:
Drawing2DMatrix newMatrix = myMediaMatrixInstance.ConvertToDrawing2DMatrix();
Just a suggestion.
I'd say that's the best way. The System.Drawing matrix is stored in unmanaged memory and the WPF matrix is a struct on the managed stack, so any trickery in doing a block copy would require some unsafe code for very little, if any, perf improvement.

Categories

Resources