![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/513942/image.png)
uiimage introduces artifacts? How to fix?
2 次查看(过去 30 天)
显示 更早的评论
I am building an application using App Designer and I am using images (Type = 'uiimage') and using the ImageSource property to display data. I am seeing artifacts on the GUI that are not in my data.
I can also produce the same behavior with a plain uiimage from the console.
img = ones(160, 160, 3, 'uint8') * 128;
img([48, 65], 1:4:end, :) = 0;
img([48, 65], 2:4:end, :) = 0;
fig = uifigure;
im = uiimage(fig);
im.ImageSource = img;
im.Position = [ 100, 100, 160, 160];
im.ScaleMethod = 'none';
The result from this is shown here:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/500738/image.png)
Looking closely at the dashed lines shows the artifacts:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/500743/image.png)
These look to me like jpeg artifacts. I am using ScaleMethod 'none' to try to rule out any resampling effects. In my application I am using a fixed size image that exactly matches the data size so it should not be performing scaling.
One of the purposes of my application is for users to closely scrutinize an image to look for artifacts in the underlying data, so it is important that the MxNx3 uint8 array is faithfully produced on the screen.
A "regular" figure with imshow does not show artifacts:
fig2 = figure;
imshow(img);
What's the best way to show the image in my application without artifacts?
采纳的回答
Nitin Kapgate
2021-2-10
编辑:Nitin Kapgate
2021-2-10
Thank you @Jamie for bringing the issue of artifacts in the uifigure + uiimage workflow to my attention.
After revisiting the issue again, I could see artifacts being displayed when the image is displayed using the uifigure + uiimage workflow.
I have brought this issue to the concerned people and the issue might be resolved in future releases.
2 个评论
Nitin Kapgate
2021-2-11
As a workaround, you can serve cdata as file:
imwrite(img, 'imgFile.png');
im.ImageSource = 'imgFile.png'
更多回答(1 个)
DGM
2024-6-26
Interesting that this had already been reported before I reported it as a bug.
The reason for these artifacts is simple. Whenever uiimage() is given a filename, it reads the image using imread(). That means that it can read a broad range of filetypes and it can support indexed images and alpha content.
The other way to use uiimage() is to feed it image data from the workspace. In this case, it takes your image, writes it to disk as a low-quality 4:2:0 subsampled JPG, then it reads it back using imread(). Not only is the image unnecessarily damaged, the fact that it uses JPG means that it can't actually display indexed or transparent images.
This thread is where I first figured this out. The followup comments describe the problem, I give some demonstrations, and demonstrate a workaround.
As to whether this constitutes a bug, I'll reiterate what I said in the bug report. Are heavily lossy image facsimiles acceptable for display purposes? No. Not when the purpose of the displayed image is to monitor a technical image editing process and examine its details and exact pixel values. If Photoshop or GIMP or MSPaint wouldn't do it, then a technical environment like MATLAB shouldn't. The fact that it's trivial to modify the support files to use PNG for the temp file is enough for me to assert that there's no justification for the damage done to the working image. That's a bug.
The question remains whether there's any justification for using any tempfile. Even if there is, PNG would allow uiimage() to support transparency and indexed images from the workspace -- but since everything was written around using garbage JPG, there isn't any in place to handle the required extra arguments. I'm doubtful that a tempfile can't be avoided.
2 个评论
DGM
2024-6-26
Oh wow. I hadn't even given speed a consideration, but yeah. That would be a great way to slow things down. I would suspect that the tempfile directory is on ramdisk, but maybe they aren't always. Either way, I don't see any reason for all the overhead of encoding/decoding.
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!