我有一个很艰难的时期工作了这一点。
在code应该从一个文件对话框导入图像。和每个图像应该是处理和发送到类正确的。 处理器是检测造型类,所以基本上我送每一个形象,并检测它的形状(类处理器内过滤到一定的标准)
newList将得到所有形状的中心的图像。
我在平行的小知识,我似乎无法弄清楚如何工作了这一点。 请记住,我并不需要从一个循环传递什么到另一个。我只想图像处理和纠正每次的时间,与整个操作分为主题。
我每次迭代一个独立的之一,我不需要从一个迭代到另一个返回任何东西。
目前的问题是,结果从类返回正确有时不正确。我想这是因为处理器和newList还必须是本地的?如果是的话我怎么能解决这个问题?如果不是我在哪里出了错?
另外请记住,使用普通的foreach工作得很好
下面是我的code:
Parallel.ForEach(ofd.FileNames,
(文件)=>
{
图片考试= Image.FromFile(文件);
VAR cvImage =新图片< BGR,字节>((位图)考试);
处理器= processorMain;
processor.ProcessImage(cvImage);
名单<点> newList =新名单,其中;点>();
newList = processor.getList();
correct.correct(cvImage,answerKey,nOptions);
});
解决方案
在注释中的实际问题/问题指出:
我每次迭代一个独立的之一,我不需要从一个迭代到另一个返回任何东西。
在这种情况下,你不希望你的图片
s到是线程本地的,你只需要他们的地方。因此该解决方案是简化:
Parallel.ForEach(ofd.FileNames,
(文件)=>
{
VAR cvImage =新图片< BGR,字节>((位图)考试);
处理器= processorMain;
processor.ProcessImage(cvImage);
名单<点> newList =新名单,其中;点>();
newList = processor.getList();
correct.correct(newList,cvImage,answerKey,nOptions);
});
但你的code没有真正使用文件
任何地方,所以这只是一个粗略的猜测。这不可能是正确的呢。
而在另一方面,的用法 processorMain
, answerKey
和 nOptions
的潜在问题。
和之后多了一些意见,你需要的是:
的IList<图像>结果= ofd.FileNames
.AsParallel()
。选择((文件)=>
{
图片考试= Image.FromFile(文件);
...
返回考试;
})了ToList()。
I'm having a really hard time working this out.
The code is supposed to import images from a file dialog. And each image is supposed to be processed and sent to class correct. Processor is a class that detects shapes, so basically I am sending every image and detecting shapes in it(filtered to a certain criteria within the class processor)
newList will get the centers of all the shapes in an image.
I have little knowledge in Parallelism and I can't seem to figure how to work this out. Keep in mind that I do not need to pass anything from one iteration to another. I just want images to be processed and corrected each at a time, with the entire operation divided to threads.
I have every iteration a stand alone one and I need not return anything from one iteration to another.
Currently the problem is that the results returned from class correct are sometimes incorrect. i guess it is because processor and newList must also be local? If yes how can I solve this? If not where have I gone wrong?
Also keep in mind that using a normal foreach works just fine
Here is my code:
Parallel.ForEach(ofd.FileNames,
(file) =>
{
Image exam = Image.FromFile(file);
var cvImage = new Image<Bgr, byte>((Bitmap)exam);
processor = processorMain;
processor.ProcessImage(cvImage);
List<Point> newList = new List<Point>();
newList = processor.getList();
correct.correct(cvImage, answerKey, nOptions);
});
解决方案
The actual question/problem was stated in a comment:
I have every iteration a stand alone one and I need not return anything from one iteration to another.
In that case you do not want your Image
s to be Thread-local, you just need them local. So the solution is to simplify:
Parallel.ForEach(ofd.FileNames,
(file) =>
{
var cvImage = new Image<Bgr, byte>((Bitmap)exam);
processor = processorMain;
processor.ProcessImage(cvImage);
List<Point> newList = new List<Point>();
newList = processor.getList();
correct.correct(newList, cvImage, answerKey, nOptions);
});
But your code is not really using file
anywhere so this is just a rough guess. It cannot be correct yet.
And on the other hand, the usages of processorMain
, answerKey
and nOptions
are potential problems.
And after a few more comments, what you need is:
IList<Image> result = ofd.FileNames
.AsParallel()
.Select( (file) =>
{
Image exam = Image.FromFile(file);
...
return exam;
}).ToList();
相关推荐
最新文章