I should be able to save it in any format with enough resolution. If I have a 2000x5000 pixel image, I should be able to save a bmp, or png or eps file with that resolution. And indeed I can with, say, imwrite or similar functions. The problem is displaying that image as a figure, then saving that figure. I can't get that figure to preserve the 2000x5000 pixel resolution - I can only get the displayed resolution of my screen (1900x1080) so it combines/merges/downsamples the underlying data pixels because they clearly can't be displayed.
Info
此问题已关闭。 请重新打开它进行编辑或回答。
Any way to preserve the native resolution of data when saving a figure?
1 次查看(过去 30 天)
显示 更早的评论
I've seen many ideas about changing paper size and the adjusting "-r" parameter, but it appears that all this does is take the on-screen displayed resolution and reproduce that at finer resolution. It doesn't get to the issue of saving a figure, whose data resolution is, say 1800x3200, to an image format that preserves that resolution on zoom. Is there any way to force MATLAB to save the figure as an image that would preserve that native high resolution - the way the .fig file would?
17 个评论
JDC
2017-10-12
Follow up - I know there is a border around the figure and that takes some resolution as well. But for a 2000x5000 pixel image, I should be able to save a 4000x10000 image of a figure including the border and still preserve the 2000x5000 data.
JDC
2017-10-12
编辑:John Kelly
2017-10-30
Thanks for your attempt.
It appears that the functionality I'm looking for does not exist due to the limitations of the print command. The very excellent export_fig is similarly constrained as it is a wrapper to the print command.
I'm not interested in a .fig file. It may be nitpicking but fig could be an abbreviation for figure, whereas .fig is an extension.
Imwrite does not preserve changes made to an axis since it is just the image and I tried very hard to indicate that I was interested in an image of a figure ... not an image of the data.
Finally, it is entirely possible that a satisfying answer does not exist.
Jan
2017-11-14
@JDC: I'm still convinced that the problem can be solved easily, as soon as it is exactly defined, what you want.
Walter Roberson
2017-11-14
JDC: A lot of computing is based upon opinions. Computing does not have only one possible measurement such as "Code quality is universally defined as being inversely proportional to the number of commas in the code.| As soon as there are at least two competing measures having to do with code, it becomes a matter of opinion as to how to what "better" code is. Therefor opinion has a big place in computing, and we will not delete opinion comments unless they are abusive.
JDC
2017-11-14
@ Walter Roberson I'll save you the trouble - MATLAB will just edit them later anyway:
This does not add anything meaningful to the technical issue at hand and should be omitted.
JDC
2017-11-14
Opinions that are contrary to facts are not helpful. There is no solution for this problem as it currently stands. Period. Full stop. To suggest otherwise is beyond an opinion, it is unhelpful speculation. So, in that regard, this whole thread is unhelpful. Cam successfully identified the problem but that apparently isn't good enough for some folks. If people don't understand the problem, they should stop trying to supply an answer. And I'm certain these opinion comments will be deleted and they are certainly not abusive.
Walter Roberson
2017-11-14
JDC: it was not until a couple of hours ago that you clearly defined what you were trying to do. You knew what you meant, but something went missing in-between your intent and the words you had posted, which led to the situation where it was indeed reasonable for people to think there was a solution. They may have been mistaken in that assessment because of missing information, but it was reasonable.
JDC
2017-11-14
编辑:JDC
2017-11-14
Wrong. It was clearly presented several weeks ago until I had to deal with people, such as yourself, injecting opinions without understanding the problem. Here's some great advice often mis-credited to Einstein: Given an hour to solve a problem, use 55 minutes to understand the problem and 5 minutes to solve it. The rush to jump on a solution and not understand the clearly written words caused a cascade of braggadocio and ridiculousness, not at all unlike this thread, that obfuscated the original intent. The problem is one of context and very few people stop to understand the actual written word and fill in their own understanding. Words like data, resolution, images ... they mean different things to different people. People aren't mistaken because of missing information but rather assumed information. What went missing was the fact that it was clear the thread had been hijacked by people who were not thinking about the problem but rather quickly throwing out solutions in an effort, I suspect, to get some brownie points. Now, if you're quite done, it seems the problem was correctly understood by Cam even though it is still not correctly understood by others. So if it were clearly defined now as you indicate, everyone should now be able to understand it ... yet inexplicably, there is still confusion among some of us. Indeed, the very same people who cried "I don't understand" are STILL crying "I don't understand". Chew on that for a couple hours before you respond.
JDC
2017-11-15
编辑:Walter Roberson
2017-11-15
First, let's make sure everyone knows what we're dealing with. Before your HEAVILY edited reply above, it looked like this --------- There is nothing wrong with "I don't understand". I still do not find a clear statement, which defines uniquely what you want. I wrote:
I'm still convinced that the problem can be solved easily, as soon as it is exactly defined, what you want.
Instead of wasting time with the discussion, that this is an opinion, it would be more useful, if you write an exact definition of your need. Try it in one sentence, such that even I can understand it. Perhaps:
When I have an 7000x7000 image and display it in a figure, how can I save it as a raster image with 7000x7000 pixels?
The tedious discussion in this thread is caused by the fact, that such an explanation is missing. If you cannot explain clearly, what you want, do not blame me for asking questions for clarifications.
If such a clear definition is given, I can or cannot convert my opinion to code. In this thread you have insisted a long time on not giving a clear definition, because you had the opinion (!) that there is no solution. This is in ... ---------
Now, some definitions. A figure is a MATLAB object file. It contains the data and a significant amount of other parameters like the boundary and axes information. Lots of child fields. I'd like to assume you know this but it sounds like you are conflating the issue of a figure (a .fig file) and an image (a matrix of data). Now, here's the really important one - resolution. Resolution is the ability to determine the difference between two objects closely positioned in space. So a two by two matrix (image) say a blue pixel, a red pixel, a green pixel and a yellow pixel is not more resolved if each of those pixels is now represented by 100 pixels, making the new image 200x200. That's not higher resolution. It's redundant information and does nothing to resolve the underlying data. What you keep suggesting is to take an image as displayed on the screen and saving that "figure" as an "image" at higher "resolution". I'll say it one last time, MATLAB is constrained by what it can display on window device by the operating system. Maybe you know about device contexts (DCs) maybe you don't, but a DC is a significant component in displaying an image on a screen for any operating system (Windows, OSX, Linux, whatever). Matlab will NOT, indeed clearly CANNOT, take the data associated with the figure file and display it on a screen that has fewer pixels than it has data points. So, if you have a 7000x7000 data matrix you wish to display as a figure, you can, but it will be grossly undersampled. But the figure file has the data so you can zoom in on the data right down to the pixel. You cannot create an arbitrarily large print object to map your underlying data from a .fig file to an image file (like a png or bmp or yes, even a vector graphics file type like eps or svg) that preserves that 7000x7000 matrix of data. The best you can do is display it on a large screen, so the operating system can create a large enough DC that can then display the data as pixels which you can then save as an image.
Now, you may not understand that but I'm telling you what you are claiming is incorrect. You provided no code that addresses this issue. I stated on day one that I'm VERY familiar with the print command. I stated specifically that I was familiar with -r parameter of this method. That should have given an indication that we are not dealing with a basic issue. You have been a hinderance and an unpleasant one at that. You post derogatory and backhanded comments then quickly edit them, likely understanding that what you've written was inappropriate but not until after the reply was sent.
Cam has correctly identified the issue. Read his response. If you don't like it, tough. If you'd like to educate yourself regarding this phenomenon, by all means, load a very large image into MATLAB. A real image, not a random matrix as you'd have no way to observe the lack of coherence since, by definition, the two random matrices - the saved one and the original - are incoherent. Something significantly larger than your screen size - like this http://effigis.com/wp-content/uploads/2015/02/Airbus_Pleiades_50cm_8bit_RGB_Yogyakarta.jpg. Verify that you can zoom in on the "figure" right down to the pixel. Then zoom back out. Now, save that figure as an image anyway you like to preserve as much data as you can. When you now display that "image" in MATLAB as a "figure" you will discover that you cannot zoom down to the pixel. It will be block processed - meaning all your extra resolution did was over resolve the under resolved figure.
Now, I'm at the end of my patience. I've been using MATLAB for over 20 years and very nearly always find a way around a problem or identify a limitation in MATLAB itself. This is a limitation, since I can preserve more "resolution" from a "figure" on a larger screen than I can the same data on a smaller screen using the exact same parameters.
I can promise you one thing - I regret ever coming to this forum.
Jan
2017-11-15
编辑:Jan
2017-11-15
@JDC: Almost 100'000 other contributors had more success with their questions in this forum, maybe because they do not take weeks before the problems is defined, answer questions for clarifications and stay at a businesslike discussion.
I'm not interested in participating in such an impolite discussion. Therefore I have removed my comments and the trial to solve or narrow down the problem with code.
JDC
2017-11-15
Maybe. More likely that there are people who understand computer science more than you or who are able to admit they don't understand the problem without being petulant. So the problem has been defined now? Hmmm, it was pretty clear you STILL didn't understand the problem. I'll take your removal as a sign that your approach was wrong, your suggestions were unhelpful, and your "businesslike" responses were less-than-businesslike.
JDC
2017-11-15
Yet you keep coming back to continue to "edit" your responses. Yes, that's much more polite. And clever too, since the forum doesn't get to see your original less-than-polite responses. So I hope you are in fact done since there are people who have to go back to solving problems with rigor and understanding.
此问题已关闭。
回答(1 个)
Cam Salzberger
2017-10-12
Hello,
It's a little hard for me to understand what you are asking for. You want an image that, when you zoom in on it, it increases its resolution (so you don't see chunky pixels)?
In that case, it sounds like you're looking for vector format images. Any raster format (JPG, PNG, etc.) will have a fixed resolution. When you zoom in, a single data point (pixel) of the image will grow to fit multiple pixels on the screen. A vector format (EPS, PDF, etc.) stores data on the "objects" within the image, rather than the pixels themselves. Thus, when you zoom in on a line, it recalculates what the line should look like on-screen, and will display it smoothly.
See the print documentation page (specifically the section on Vector Graphics Files) for information on your options.
-Cam
25 个评论
Cam Salzberger
2017-10-12
Ah, I understand. However, why are you trying to use "print" to save the image data rather than imwrite? If you have the image data already, it's invariably better to save it directly rather than putting it onto a figure and printing it.
Have you done some other manipulation to the figure/axes that you are hoping to preserve in the image? If so, what kind (e.g. drawn a single circle, drawn a box, arbitrarily allowed the user to scribble anywhere, etc.)?
Cam Salzberger
2017-10-12
Alright, so you're looking to save the figure to a FIG file, but maintain the resolution. I think I understand now.
One workaround I can suggest is to still use imwrite to create a separate image file. Then add a CreateFcn callback to read in the image from file and display it on the axes. Then clear the axes before saving the figure (if you want to).
It would mean tracking two files rather than just one, but you'll preserve the image size.
Hope this does it for you!
-Cam
JDC
2017-11-14
I want to save a figure as an image, i.e. figure->save as->.png, .eps, .jpg, etc. The problem is that the best achievable resolution of that save function is dependent on the screen resolution. So, a very high resolution monitor, say 4k, will allow you to preserve much more of the resolution of the underlying data set than a laptop monitor will, since the laptop monitor can't display more data than it has pixels. If you have a large image file, it will be severely compressed. As far as I can tell, there is no mechanism to "trick" MATLAB into believing it is connected to a large monitor and allow you to save a figure as an image using this new monitor resolution. For modifications to an image, say the addition of axes, titles, or markings, using imwrite will not preserve these changes. And, if you simply saveas or even export_fig, the data will only be saved with the monitor resolution.
Cam Salzberger
2017-11-14
Alright, I think I understand now, and can reproduce it to a certain extent. You have a very large image that you would like to annotate with axes-based graphics objects, then convert back to an image file. I believe the underlying issue comes from the warning you see when you display the image:
Warning: Image is too big to fit on screen; displaying at 17%
When you go to save the image, getframe and File -> Save As will provide it at the screen resolution size. saveas will provide it at a fixed 150 dpi, and if you scale up the resolution in print, it will simply stretch the displayed pixels and make the resulting image choppy. Keeping the resolution at screen resolution and expanding the size of the image through the 'PaperPosition' property has similar results.
Windows itself will prevent any program from making its window size larger than screen size, so you can't simply change the figure's 'Position' property to be as big as you need. You may be able to get around that somewhat if you have multiple monitors though. Windows (I believe) limits the size of windows to the combined width of the displays, and the largest height of the displays. So you may be able to push that boundary on width somewhat. However, it's a workaround at best.
You could, potentially, display just part of the image at a time. Or display the whole thing, annotate, and play with the axes limits to only view part of the image at a time. Finally, use getframe on the axes, and concatenate the resulting images together in a final large image. It's not an ideal workflow, but it could get around the issue.
I will put in an enhancement request with the appropriate team, including your workflow and the limitations you are running into. Our development teams will look into potentially including a method to meet your workflow in a future release of MATLAB.
-Cam
Rik
2017-11-14
I can set the position of a figure outside the bounds of the monitor before writing, so this might be the trick you're looking for.
f=figure(1);
plot(rand(2,4))
p=get(0,'screensize');
set(f,'Position',[1 1 p(3:4)*2])
drawnow
saveas(f,'test1.png')
set(f,'Position',[1 1 p(3:4)/2])
drawnow
saveas(f,'test2.png')
JDC
2017-11-14
I think you get that warning with imshow but I've never seen it with imagesc since I usually scale the images. The other methods only print a higher resolution of the low resolution screen image. So, you have 4 pixels representing the same information of one pixel of data, which is not what I'm after. I can set the paper size to any arbitrary size I want but the only way I can get MATLAB to actually preserve the resolution of the data to anything close to native (i.e. 7000x7000 matrix) is by displaying it on my 4k monitor. I can get a much better representation of it saving from that monitor than I can from the laptop, which tells me it's an issue of mapping the data to the pixels. Simply making a larger canvas, like changing the size of the paper, only maps what's displayed. It seems what's missing is some type of emulation so that MATLAB can be tricked into thinking it's displaying each data point to a pixel on a screen, without actually having the screen.
JDC
2017-11-14
And I think you have it right, Cam. Right now, I can break the image into smaller pieces, or zoom and save just a portion. I can get arbitrarily better results playing with the print settings but those are mostly just how the data "looks" upon display in another editor, i.e. Photoshop, Paint, Gimp, etc.
JDC
2017-11-14
Saving an image with 1200 dpi resolution for a 10x10 dataset where one pixel equals one 10th of an inch does not make it a high resolution image of the dataset. That resolution is redundant and can't provide any more meaningful information than saving it with 100 dpi. Likewise, if you have an image that is 7000x7000 pixels where each pixel is a tenth of an inch, all you will do by saving it with 1200 dpi resolution is make a higher resolution copy of what is displayed by your monitor. That is a fact and as Cam noted, a limitation with the interaction with the OS.
Jan
2017-11-14
编辑:Jan
2017-11-15
@Cam:
Windows itself will prevent any program from making its window
size larger than screen size
While Windows does not restrict the size, it is Matlab who does it, unfortunately. See https://www.mathworks.com/matlabcentral/fileexchange/31437-windowapi:
FigH = figure;
WindowAPI(FigH, 'Position', [100, 100, 5000, 200]);
get(FigH, 'Position')
>> [100, 100, 5000, 200]
Maybe it matters that JDC works on a Mac.
Walter Roberson
2017-11-15
One process for adding information to large images and saving them with the same size is to use the Computer Vision toolbox insertShape() or insertText() or insertObjectAnnotation(), and imwrite() the result.
This approach is not the most pleasant for merging plots such as surfaces with images: it does require rendering those at the target resolution, grabbing the frame, and merging the captured information in. If the item to be merged exceeds screen resolution when drawn at target resolution then there could be difficulty.
JDC
2017-11-15
I work on all platforms - my Mac Retina has the highest laptop pixel density and allows me to save with the most resolution. I have a Windows/Linux dual boot and I use 3 4k monitors connected to an eGPU in turn connected to a Dell 7720. I use this setup when not traveling. This behavior is universal across platforms.
Cam Salzberger
2017-11-15
Jan Simon,
It was my understanding that Windows at least tries to prevent windows becoming larger than display size, though I may have misunderstood (I am not a graphics expert). I have observed the behavior with other programs when simply trying to resize their window to be larger than screen size (e.g. Chrome). There appear to be ways around the limitation if you specifically try to do that. I would guess that's what the WindowsAPI submission does, though I haven't taken more than a cursory glance at it. Definitely a viable option to try though.
Walter's idea for editing the image directly is definitely another viable alternative, depending on what kind of annotations you are looking to do.
-Cam
Jan
2017-11-15
@Cam: Yes, WindowAPI.mex is simply a wrapper function to the Windows API function SetWindowPos(). This can be used e.g. to create a window, which covers 3 monitors to display a movie in the 25:9 format, e.g. using the VLC player.
Matlab's ResizeFcn limits the size of a figure to the current screen - to be exact, to the usable area without the taskbar. Therefore setting the position to [0,0,1,1] does not create a full screen view of the inner position, but the outer position is set to the screen size. Therefore WindowAPI.mex uses a flag, which hides the change of the window size, such that Matlab's ResizeFcn is not triggered.
While a huge figure can be created using this method, Matlab's PRINT fails with a Java error during trying to grab its contents. The figure's contents can be grabbed by the Windows API directly also. E.g. a screenshot of the OS using Ctrl-Print works for a multi-monitor setup - even for monitors which are not existing physically. But on one hand JDC is not interested in my assistance to solve his problem, obviously. On the other hand it seems like Matlab figures are not a convenient way to insert annotations in high-resolution images. Therefore I'd follow Walter's suggestion or use e.g. ImageMagick.
JDC
2017-11-15
Remember this just a few short hours ago "I'm not interested in participating in such an impolite discussion. Therefore I have removed my comments and the trial to solve or narrow down the problem with code." Yet, here you are AGAIN, and AGAIN, ... and AGAIN. Do you have some problem that prevents you from not mentioning me? You think this "But on one hand JDC is not interested in my assistance to solve his problem, obviously." is not spam? Are we living in alternate universes communicating through some wormhole because I'm starting to get that impression. Here, let me fix this for you:
While a huge figure can be created using this method, Matlab's PRINT fails with a Java error during trying to grab its contents. The figure's contents can be grabbed by the Windows API directly also. E.g. a screenshot of the OS using Ctrl-Print works for a multi-monitor setup - even for monitors which are not existing physically. It seems like Matlab figures are not a convenient way to insert annotations in high-resolution images. Therefore I'd follow Walter's suggestion or use e.g. ImageMagick.
You're welcome.
JDC
2017-11-15
编辑:JDC
2017-11-15
This ^^. Unsolicited communication via electronic communications.
Let me guess - you have one of those internet connections that only allows you to access MATLAB forums and harass folks. Otherwise, unless you're using one of those voice to text machines, you could type into your favorite web browsers SPAM DEFINITIONS and you could amuse yourself for hours. Or you could just keep "trolling" (Might as well look up that one while you're at it.)
Walter Roberson
2017-11-15
"[mass noun]
1 Irrelevant or unsolicited messages sent over the Internet, typically to a large number of users, for the purposes of advertising, phishing, spreading malware, etc.
1.1 Unwanted or intrusive advertising on the Internet.
[verb]
Send the same message indiscriminately to (a large number of Internet users)."
I see no advertising, phishing, or spreading of malware here. Furthermore, by posting in this forum you were soliciting replies.
JDC
2017-11-15
编辑:JDC
2017-11-15
https://en.wikipedia.org/wiki/Spamming
I think you have a hard time with English grammar and particles of speech. You should brush up on adverbs and prepositions, specifically. That right there might be the reason you have a hard time understanding questions people pose. Just a thought.
By the way, did you look up trolling? Just curious. I'm guessing probably not since it probably took you the better part of the afternoon to get to the bottom of spam. Hope you're retired.
Also Jan wasn't replying to me but to Cam. There was precisely zero reason to mention me. That is unsolicited. I'm guessing you missed that nuance.
Walter Roberson
2017-11-15
Nothing in that wikipedia article agrees with your use of the term "spam" with respect to any postings in this thread.
JDC
2017-11-15
Nothing except the very first line:
"Electronic spamming is the use of electronic messaging systems to send an unsolicited message (spam)"
I did not solicit a response from Jan but he provided one. I'm starting to wonder if I should simply play the game you folks seem to favor and just keep requesting an explanation.
Explain this: "Nothing in that wikipedia article agrees with your use of the term "spam" with respect to any postings in this thread." It makes no sense and needs clarification. Maybe try the print command?
Walter Roberson
2017-11-15
"I did not solicit a response from Jan"
You posted here. That solicits responses from anyone who cares to respond -- whether you like the response or not. You asked how to do something, with the something originally being unclear, and various people including Jan attempted to assist you, including by asking you to clarify statements and by proposing solutions that were in line with the information that had been presented by you. You solicited responses; you got responses. None of the responses were advertising, phishing, or spreading malware.
JDC
2017-11-16
This is unclear. You need to try to explain this again. I can help you if you just make a one line statement of what you are trying to do. Have you tried adjusting the -r parameter? I'm sure there is a simple solution to your inability to understand how solicitation works. Maybe an example. Stay tuned, I'll find one of your responses to someone else, respond to them, but deride you in the process. Or, you could just try the print function again, I hear that solves everything.
Jan
2017-11-16
编辑:Jan
2017-11-16
A newcomer drops into the forum, asks an unclear question and gets upset due to questions for clarifications. He accuses two long term high reputation users for "spamming" and "trolling" and tries to attack with off-topic insults, sarcasm and parody. Finally he announces to disturb threads of other users. There is no doubt, who acts like a troll here.
JDC, your excitement is not impressing and nobody will care about your personal opinions. Walter (mainly) and I have posted more than 45'000 answers in this forum with great success and we will proceed with our style of communication. I do not have to defend against you, how I try to help to solve a problem or what I write while talking with another user. You simply do not have the power to influence, what I write.
The customs in the forum will not be changed, because one user is not satisfied in his first thread. You can check, how this forum works, and decide for yourself if you want to participate productively - or not. this is your decision and I cannot influence it by asking some questions for clarifications.
JDC
2017-11-16
This is unclear. You need to try to explain this again. I can help you if you just make a one line statement of what you are trying to do. Have you tried adjusting the -r parameter?
This is not a meaningful task. Instead of "asks an unclear question" you should write "a question that is unclear". It does not help if the members of the forum agree with you, that there should be something, when they know, that there is one already.
Obviously you care, otherwise you'd stop posting. But you can't because you just can't get that darn print function to work. I know you said it would work but it just doesn't seem to be doing what you said it would do. Try saving at -r15000000, see if that helps.
JDC
2017-11-16
Also, why do you keep saying HE and HIS. Are you sexist and only allow males to use MATLAB?
此问题已关闭。
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!发生错误
由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
亚太
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)